Leetcode105 Construct Binary Tree from preorder and inorder traversal Java implementation

Source: Internet
Author: User

First wrote a primitive Method 1: (Java without slicing very uncomfortable AH)

/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode rig Ht * TreeNode (int x) {val = x;} }*/classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {        intLen =inorder.length; if(len==0)return NULL; TreeNode P=NewTreeNode (preorder[0]); if(len==1)returnp; intindex=0; for(; index<len;index++) {if(Inorder[index]==preorder[0]) Break;} if(index==0) {//description only right subtree            int[] Newpre =New int[Len-1];int[] Newin =New int[Len-1];  for(inti=0;i<len-1;i++) {Newpre[i]= Preorder[i+1];newin[i]=inorder[i+1]; } p.right=Buildtree (Newpre,newin); returnp; }        Else if(index==len-1) {            int[] Newpre =New int[Len-1];int[] Newin =New int[Len-1];  for(inti=0;i<len-1;i++) {Newpre[i]= preorder[i+1];newin[i]=Inorder[i]; } p.left=Buildtree (Newpre,newin); returnp; }        Else {            int[] LP =New int[index];int[] Li =New int[index]; int[] RP =New int[Len-index-1];int[] ri=New int[len-1-index];  for(inti=0;i<index;i++) {Lp[i]= preorder[i+1];li[i]=Inorder[i]; }             for(inti=index+1;i<len;i++) {Rp[i-index-1]=preorder[i];ri[i-index-1]=Inorder[i]; } p.left=Buildtree (Lp,li); P.right=Buildtree (Rp,ri); returnp; }    }}

Inefficient and heinous, but think also, every time to re-establish the array, certainly trouble ah, so optimization, get Method 2:

classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {        returnBuild (preorder,inorder,0,preorder.length-1,0,inorder.length-1); }     PublicTreeNode Build (int[] Preorder,int[] inorder,intPlintprintIlintir) {        intLen = pr-pl+1; if(len==0)return NULL; TreeNode P=NewTreeNode (PREORDER[PL]); if(len==1)returnp; intIndex=il; for(; index<ir+1;index++) {if(INORDER[INDEX]==PREORDER[PL]) Break;} P.left= Build (preorder,inorder,pl+1,pl+index-il,il,index-1); P.right= Build (preorder,inorder,pl+index-il+1,pr,index+1, IR); returnp; }}

The effect is better, but still not the first echelon.

How to optimize it? See the idea of the great God found that each time in the inorder sequence to search again, less efficient, you can establish a map storage inorder the corresponding relationship between the value and index, improve efficiency:

/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode rig Ht * TreeNode (int x) {val = x;} }*/classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {Map<Integer,Integer> Indexmap =NewHashmap<>();  for(inti=0;i<inorder.length;i++) Indexmap.put (inorder[i],i); returnBuildv2 (preorder,inorder,0,preorder.length-1,0,inorder.length-1, Indexmap); }     PublicTreeNode Buildv2 (int[] Preorder,int[] inorder,intPlintprintIlintIr,map<integer,integer>Indexmap) {        intLen = pr-pl+1; if(len==0)return NULL; TreeNode P=NewTreeNode (PREORDER[PL]); if(len==1)returnp; intindex=Indexmap.get (PREORDER[PL]); P.left= Buildv2 (preorder,inorder,pl+1,pl+index-il,il,index-1, Indexmap); P.right= Buildv2 (preorder,inorder,pl+index-il+1,pr,index+1, Ir,indexmap); returnp; }}

Into the first echelon, 1ms big guy's solution did not see too understand, left to see it later.

Leetcode105 Construct Binary Tree from preorder and inorder traversal Java implementation

Related Article

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.