LeetCode-in-Cpp

114. Flatten Binary Tree to Linked List

Medium

Given the root of a binary tree, flatten the tree into a “linked list”:

Example 1:

Input: root = [1,2,5,3,4,null,6]

Output: [1,null,2,null,3,null,4,null,5,null,6]

Example 2:

Input: root = []

Output: []

Example 3:

Input: root = [0]

Output: [0]

Constraints:

Follow up: Can you flatten the tree in-place (with O(1) extra space)?

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode* root) {
        if (root != nullptr) {
            findTail(root);
        }
    }

private:
    TreeNode* findTail(TreeNode* root) {
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        TreeNode* tail;

        // Find the tail of left subtree, tail means the most left leaf
        if (left != nullptr) {
            tail = findTail(left);
            // Stitch the right subtree below the tail
            root->left = nullptr;
            root->right = left;
            tail->right = right;
        } else {
            tail = root;
        }

        // Find tail of the right subtree
        if (tail->right == nullptr) {
            return tail;
        } else {
            return findTail(tail->right);
        }
    }
};