Simple recursive yet accepted as best in cpp


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

Log in to reply
 

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