2 times AC ...... C++


  • 0
    2

    I forget to set the left pointer of the current node to point to NULL.....

    23333333333

     class Solution {
        public:
            void flatten(TreeNode* root) {
                if(!root)  {
                    return;
                }
                
                if(!root->left)  {
                    flatten(root->right);
                    return;
                }
                
                if(root->left) {
                    flatten(root->left);
                    flatten(root->right);
                    TreeNode* last_left = help(root->left);
                    last_left->right = root->right;
                    root->right = root->left;
                    /** root->left = NULL; **/
                }
                return;
            }
            
            TreeNode* help(TreeNode* root){
                TreeNode *cur = root;
                while(cur) {
                    if(cur->right)
                        cur = cur->right;
                    else if(cur->left)
                        cur = cur->left;
                    else
                        return cur;
                }
            }
        };

Log in to reply
 

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