Flatten Binary Tree to Linked List

Source: Internet
Author: User

Flatten a binary tree to a fake "linked list" in pre-order traversal.

Here we use the right pointer in TreeNode as the next pointer in ListNode.

For example,
Given
         1        /        2   5      /\        3   4   6

The flattened tree should look like:

   1         2             3                 4                     5                         6

Analysis:

Put all the tree nodes in the order of pre-order in ArrayList, then traverse the nodes in the ArrayList and adjust their left and right child pointers.

1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9  *     }Ten  * } One  */ A  Public classSolution { -     /** - * @param root:a TreeNode, the root of the binary tree the * @return: Nothing - * Cnblogs.com/beiyeqingteng -      */ -      Public voidFlatten (TreeNode root) { +         //Write your code here -arraylist<treenode> list =NewArraylist<treenode>(); + Preorder (root, list); A          at          for(inti =0; I < list.size (); i++) { -List.Get(i). Left =NULL; -             if(i = = List.size ()-1) { -List.Get(i). Right =NULL; -}Else { -List.Get(i). Right = list.Get(i +1); in             } -         } to     } +      -      Public voidPreorder (TreeNode root, arraylist<treenode>list) { the         if(Root! =NULL) { * List.add (root); $ Preorder (root.left, list);Panax Notoginseng Preorder (root.right, list); -         } the     } +}
View Code

There is a recursive way, the following method refers to another netizen's approach, that this method is very easy to understand, and has a general.

Solution 2: Recursive construction

Suppose that the left and right subtree T (root->left) and T (root->right) of a node have been flatten into the linked list:

1
/    \
2 5
\       \
3 6 <-Righttail
\
4 <-Lefttail

How do I flatten root, t (root->left), T (Root->right) into an entire linked list? Obvious:

temp = Root->right
Root->right = Root->left
Root->left = NULL
Lefttail->right = Temp

We need to use the flatten linked list's trailing element Lefttail, Righttail. So the recursive return value should be the tail of the entire linked list that was generated.

1 classSolution {2      Public voidFlatten (TreeNode root) {3 FLATTENBT (root);4     }5 6 TreeNode flattenbt (TreeNode root) {7         if(Root = =NULL)return NULL;8TreeNode Lefttail =FLATTENBT (root.left);9TreeNode Righttail =FLATTENBT (root.right);Ten if (root.left! = null) { OneTreeNode temp =Root.right; ARoot.right =Root.left; -Root.left =NULL; -Lefttail.right =temp; the         } -         if (righttail! = null) return RIGHTTAIL;18 if (lefttail! = null) return lefttail;19 return ro OT; -     } +}

Several details to be noted
ln 10: Insert the left dial hand tree into the right subtree only if it exists
ln 17-19: When the tail element is returned, special treatment (1) is required to have the right subtree, (2) There is no right subtree but there is a case of Zuozi, (3) The absence of the subtree.

Reference:

Http://bangbingsyb.blogspot.com/2014/11/leetcode-flatten-binary-tree-to-linked.html

Flatten Binary Tree to Linked List

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.