And 105 is the same.
1 PublicTreeNode Buildtree (int[] inorder,int[] postorder) {2 if(Inorder.length = = 0 | | Postorder.length! =inorder.length) {3 return NULL;4 }5 intLen =inorder.length;6 returnHelper (inorder, Postorder, 0, len-1, 0, len-1);7 }8 9 PrivateTreeNode Helper (int[] inorder,int[] Postorder,intInstart,intInend,intPoststart,intpostend) {Ten if(Instart > Inend | | poststart >postend) { One return NULL; A } -TreeNode root =NewTreeNode (Postorder[postend]); - intRootindex =indexOf (Inorder, Postorder[postend]); the intLeftlen = Rootindex-Instart; - intRightlen = Inend-Rootindex; -Root.left = Helper (inorder, Postorder, Instart, RootIndex-1, Poststart, Poststart + leftLen-1); -Root.right = Helper (inorder, Postorder, Rootindex + 1, inend, Postend-rightlen, postEnd-1); + returnRoot; - } + A Private intIndexOf (int[] arr,intkey) { at for(inti = 0; i < arr.length; i++) { - if(Arr[i] = =key) { - returni; - } - } - return-1; in}
106. Construct Binary Tree from inorder and postorder traversal