FLATTEN BINARY TREE TO LINKED LIST easiest c++ code


  • 0
    U

    Simple solution like creating ll from tree

    TreeNode* Solution::flatten(TreeNode* A) 
    {
     if(A==NULL)
     return NULL;
     TreeNode* temp1=flatten(A->left);              //left list
     TreeNode* temp2=flatten(A->right);           //right list
     A->right=temp1;                             //connect root to l1
     A->left=NULL;                                //make left null
     if(temp1!=NULL)                            //connect l1 to l2
     {
        while(temp1->right!=NULL)
        temp1=temp1->right;
        temp1->right=temp2;
     }
     else
     A->right=temp2;                            //connect root to l2 if l1 is null
     return A;
    }
    

Log in to reply
 

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