# 106. Construct Binary Tree from inorder and Postorder traversal

Source: Internet
Author: User

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

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

Subscribe to see which companies asked this question

Hide TagsTree Array Depth-first SearchHide Similar Problems(M) Construct Binary Tree from preorder and inorder traversal
`/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode rig Ht * TreeNode (int x) {val = x;} }*/ Public classSolution { PublicTreeNode Buildtree (int[] inorder,int[] postorder) {HashMap<integer, integer> Inorderindex =NewHashmap<integer, integer>(inorder.length);  for(inti = 0; i< inorder.length; ++i) {inorderindex.put (inorder[i], i); }        returnBuildtree (inorder, 0, Inorder.length, postorder, 0, Postorder.length, Inorderindex); }        PrivateTreeNode Buildtree (int[] inorder,intInorderfrom,intInorderto,int[] Postorder,intPostorderfrom,intPostordeto, Hashmap<integer, integer>Inorderindex) {        if(Inorderfrom>=inorderto | | postorderfrom>=Postordeto)return NULL; intRootvalue = postorder[postordeto-1]; TreeNode Root=NewTreeNode (Rootvalue); intRootindex =Inorderindex.get (Rootvalue); Root.left=Buildtree (inorder, Inorderfrom, Rootindex, Postorder, Postorderfrom, Postorderfrom + (Rootindex-inorderfrom), inorderindex); Root.right= Buildtree (Inorder, rootindex+1, Inorderto, Postorder, Postorderfrom+ (Rootindex-inorderfrom), postordeTo-1, Inorderindex); returnRoot; }}`

106. Construct Binary Tree from inorder and Postorder traversal

Related Keywords:

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.