My simple c++ recursive solution


  • 0
    T
    class Solution {
    public:
        void flatten(TreeNode* root) {
            if (!root) return;
            flatten(root->left);
            flatten(root->right);
            auto ln = root->left;
            while (ln && ln->right) ln = ln->right;
            if (ln) {
                ln->right = root->right;
                root->right = root->left;
            }
            root->left = NULL;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.