106. Construct Binary Tree from Inorder and Postorder Traversal

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

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / 
  9  20
    /  
   15   7
 1 class Solution {
 2     public TreeNode buildTree(int[] inorder, int[] postorder) {
 3         return help(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
 4     }
 5     private TreeNode help(int[] inorder,int in_start,int in_end,int[] postorder,int post_start,int post_end){
 6         if(in_start>in_end||post_start>post_end) return null;
 7         TreeNode root = new TreeNode(postorder[post_end]);
 8         int j = in_start;
 9         while(inorder[j]!=postorder[post_end]&& j <=in_end)
10             j++;
11         root.left = help(inorder,in_start,j-1,postorder,post_start,post_start+(j-in_start)-1);
12         root.right = help(inorder,j+1,in_end,postorder,post_end-(in_end-j),post_end-1);
13         return root;
14     }
15 }
原文地址:https://www.cnblogs.com/zle1992/p/8379513.html