Construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

Given Preorder and inorder 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 Inorder and Postorder traversalOriginal version, easier to understand, and doing more copying work.
/*** 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[] Preorder,int[] inorder) {        if(Preorder.length = = 0)            return NULL; intRootvalue = preorder[0]; TreeNode Root=NewTreeNode (Rootvalue); if(Preorder.length = = 1)            returnRoot; intInorderrootindex = 0;  for(inti = 0; i<inorder.length; ++i) {if(Inorder[i] = =rootvalue) {Inorderrootindex=i;  Break; }        }                int[] Preorderleftbranch = Arrays.copyofrange (preorder, 1, inorderrootindex+1); int[] Preorderrightbranch = Arrays.copyofrange (Preorder, inorderrootindex+1, preorder.length); int[] Inorderleftbranch = Arrays.copyofrange (inorder, 0, Inorderrootindex); int[] Inorderrightbranch = Arrays.copyofrange (inorder, inorderrootindex+1, inorder.length); Root.left=Buildtree (Preorderleftbranch, inorderleftbranch); Root.right=Buildtree (Preorderrightbranch, inorderrightbranch); returnRoot; }}


Improved version. Avoid array copying and improved root index look up.
/*** Definition for Binary tree * public class TreeNode {* Int. val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} }*/ Public classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {Map<integer, integer> inmap =NewHashmap<integer, integer>();  for(inti = 0; i < inorder.length; i++) {inmap.put (inorder[i], i); } TreeNode Root= Buildtree (preorder, 0, preorder.length-1, inorder, 0, Inorder.length-1, Inmap); returnRoot; }         PublicTreeNode Buildtree (int[] Preorder,intPrestart,intPreend,int[] inorder,intInstart,intInend, Map<integer, integer>Inmap) {        if(Prestart > Preend | | instart > Inend)return NULL; TreeNode Root=NewTreeNode (Preorder[prestart]); intInroot =Inmap.get (Root.val); intNumsleft = Inroot-Instart; Root.left= Buildtree (Preorder, Prestart + 1, Prestart + numsleft, inorder, Instart, inRoot-1, Inmap); Root.right= Buildtree (preorder, Prestart + numsleft + 1, preend, inorder, Inroot + 1, Inend, Inmap); returnRoot; }}

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.