[leetcode#105] Construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User
Tags chop

The problem:

Given Preorder and inorder traversal of a tree, construct the binary tree.

Note:
Assume that duplicates does not exist in the tree.

My Analysis:

The idea behind this problem is really elegant and meaningful!
We need to combinely use both preorder and inorder arrays.
Rucursion:
The first node in preorder[] must is the root of preorder array.
Use this information, we could separate the inorder[] to left sub-tree and right sub-tree.

One important thing we need to hold is so, we should guarantee the elements of preorder array and inorder array are the same!
How to guarantee it?
We should use the connection between preorder and inorder traversal.
Prorder SERIES:ABCDEFGH
Inorder SERIES:BCDAEFGH
From the preorder series, we could conclude, which is the root of those nodes. From the Inorder series, we could conclude that BCD is the nodes belonged to left Sub-Tree, and EFGH is the nodes belong Ed to the right sub-tree.
Even though we can ' t assure the exeact poistion of BCD in the left sub-tree, we can assure that they must appear ahead of All nodes in the right sub-tree. Thus BCD must appear ahead in the preorder series before efgh. (the left Sub-Tree elements must appear before right sub-tree elements in both Prorder series and Inorder series)

By using the above inference, we could guarantee we can get the same set of elements both in preorder series and inorder s Eries.

[Inorder_low, Index-1]   [index + 1-preorder_low-1 = index-1-Inorder_low ====> preoder_high = Preorder_low + index-inorder_low

(The elements in the left sub-tree must equal to elements both in Inorder series and preorder series)
Note:in Preorder Series, we chop off the first element. While in Inorder series, we chop off the index (TH) element.
Left Sub-trees Preorder Series:

[Preorder_low + 1, preorder_low + index- inorder_low]right sub-+ index-inorder_low + 1, Preorder_high ]

What's an interesting question!

My Solution:

 Public classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {                if(Preorder = =NULL|| Preorder.length = = 0)            return NULL; if(Inorder = =NULL|| Inorder.length = = 0)            return NULL; intLen =inorder.length; HashMap<integer, integer> order_map =NewHashmap<integer, integer>();  for(inti = 0; i < inorder.length; i++) {order_map.put (inorder[i], i); }                returnHelper (preorder, 0, len-1, inorder, 0, len-1, Order_map); }        PrivateTreeNode Helper (int[] Preorder,intPre_low,intPre_high,int[] inorder,intIn_low,intIn_high, Hashmap<integer, integer>Order_map) {                if(Pre_low > Pre_high | | in_low >In_high)return NULL; TreeNode ret=NewTreeNode (Preorder[pre_low]); intindex =Order_map.get (Preorder[pre_low]); Ret.left= Helper (preorder, Pre_low + 1, Index-in_low + Pre_low, inorder, In_low, index-1, Order_map); Ret.right= Helper (preorder, Index-in_low + Pre_low + 1, Pre_high, inorder, index + 1, In_high, Order_map); returnret; }}

[leetcode#105] Construct Binary Tree from preorder and inorder traversal

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.