"Leetcode" Flatten Binary Tree to Linked List (middle)

Source: Internet
Author: User

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1        /        2   5      /\        3   4   6

The flattened tree should look like:

   1         2             3                 4                     5                         6

Ideas:

With the first sequence traversal, the order is obtained from small to large. To deform the first order traversal:

voidFlatten (treenode*root) {        if(NULL = = root)return; Vector<treenode *>v;        V.push_back (root); TreeNode* Pcur =NULL; TreeNode* p =NULL;  while(!V.empty ()) {Pcur= P =V.back ();  while(NULL! = p->left)//Move to the left as long as you move the current pointer, because it is in the order of the small{v.push_back (P-Left ); Pcur= P = p->Left ; }             while(!v.empty () && (P = v.back ()->right) = = NULL)//find the next element to be processed, must be a right sub-treeV.pop_back (); if(!v.empty ()) V.pop_back (); if(P! = NULL)//The right subtree to be processed is connected to the left subtree of the currently flattened number{v.push_back (P); Pcur->left =p; }} P=Root;  while(P! = NULL)//mirroring, all transferred to right subtree{p->right = p->Left ; P->left =NULL; P= p->Right ; }    }

I find that I rely too much on non-recursion, in fact sometimes better with recursion. Like the Great God version:

Private NULL ;  Public void Flatten (TreeNode root) {    ifnull)        return;    Flatten (root.right);    Flatten (root.left);     = prev;     NULL ;     = root;}

First put the right subtree flat, and then the left sub-tree flat. Because the right subtree is dealt with every time, Prev is a record of values from large to small, and each time prev is the position of the pointer that is slightly larger than the current root node. (not very well understood)

The following is like the idea above, but it is easy to understand a lot:

 public  void   Flatten (TreeNode root) { if  (root = 
   
    null ) 
    return  
    ;        TreeNode left  =
     Root.left;        TreeNode right  =
     Root.right;        Root.left  = 
    null  
    ;        Flatten (left);        Flatten (right);        Root.right  =
     left;        TreeNode cur  =
     root;  
    while  (cur.right! = 
    null ) cur =< Span style= "color: #000000;"        > Cur.right;    Cur.right  =
     right; }
   

It is also the first to pave the right sub-tree, and then pave the left subtree. Must be the right subtree value is greater than the left subtree, so first put Shoren to the right side of the root node, and then the right subtree attached to the back. This code tiles the order of the left and right subtrees without restriction. The reverse is OK.

The non-recursive version of the above ideas: also need to think half a day to understand. Each time you first connect the subtree, the other code is connected from the leaf node, which starts at the root node.

 Public voidFlatten (TreeNode root) {if(Root = =NULL)return; Stack<TreeNode> STK =NewStack<treenode>();        Stk.push (root);  while(!Stk.isempty ()) {TreeNode Curr=Stk.pop (); if(curr.right!=NULL) Stk.push (curr.right); if(curr.left!=NULL) Stk.push (curr.left); if(!stk.isempty ()) Curr.right=Stk.peek (); Curr.left=NULL;//dont forget this!!        }    }

"Leetcode" Flatten Binary Tree to Linked List (middle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.