不断递归的实现!!!!
下面是AC代码:
1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || postorder ==null 9 || inorder.length == 0 || postorder.length == 0)10 return null;11 if(inorder.length!=postorder.length)12 return null;13 if(inorder.length == 1)14 {15 if(inorder[0] != postorder[0])16 return null;17 else18 return new TreeNode(inorder[0]);19 }20 // make sure how many nodes in the left21 int i=0;22 while(inorder[i++] != postorder[postorder.length-1]);23 int[] leftInorder = new int[i-1];24 int[] leftPostorder = new int[i-1];25 int j = 0;26 while(j