Accepted C++ pre-order recursion solution


  • 0
    D
    void flatten(TreeNode *root) {
        if(root==NULL || (root->left==NULL && root->right==NULL)) return;
        if(root->left==NULL) {
            flatten(root->right);
            return;
        }
        
        TreeNode* rNode = root->right;
        root->right = root->left;
        root->left = NULL;
        flatten(root->right);
        
        TreeNode *pNode = root;
        while(pNode->right!=NULL)
            pNode = pNode->right;
        pNode->right = rNode;
        flatten(rNode);
    }

Log in to reply
 

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