/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Converter {
  public:
    ListNode* treeToList(TreeNode* root) {
        // write code here
        if (!root) return nullptr;
        auto dummy_root = new ListNode(0);
        dummy_root->next = treeToList(root->left);
        auto cur = dummy_root;
        while (cur->next) {
            cur = cur->next;
        }
        cur->next = new ListNode(root->val);
        cur = cur->next;
        cur->next = treeToList(root->right);
        return dummy_root->next;
    }
};