C++ 9 lines 8ms recursive solution


  • 0
    B
    class Solution {
    public:
        void flatten(TreeNode* root) 
        {
            if(!root) return;
            flatten(root->left), flatten(root->right);
            if(!root->left) return;
            TreeNode * temp = root;
            while(temp -> left) temp = temp -> left;
            while(temp->right) temp = temp->right;
            temp -> 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.