Easy c++ code 19 lines


  • 1
    L
    void dfs(TreeNode *root,TreeNode* &pre){
        if(root==NULL) return;
        dfs(root->left,pre); 
        TreeNode *tmp = root->right;
        if(root->left){
            pre->right=root->right;
            root->right=root->left;
            root->left=NULL;
        }else{
            pre = root;
        }
        dfs(tmp,pre);
    }
    void flatten(TreeNode* root) {
        TreeNode *pre=NULL;
        dfs(root,pre);
    }

Log in to reply
 

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