https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
http://blog.csdn.net/linhuanmars/article/details/24390157
/** * definition for binary tree * public class treenode { * int val; * TreeNode left; * treenode right; * treenode (int &NBSP;X) { val = x; } * } */public class solution { public treenode buildtree (int[] inorder, Int[] postorder) { if (inorder == null | | inorder.length == 0 | | postorder == null | | postorder.length == 0 | | inorder.length != postorder.length) return null; map<integer, integer&gT; inordermap = new hashmap<> (); for (int i = 0 ; i < inorder.length ; i ++) { inordermap.put (Inorder[i], i); } return build (0, inorder.length - 1, postorder, 0, POSTORDER.LENGTH&NBSP;-&NBSP;1,&NBSP;INORDERMAP); } private treenode build (Int instart, int inend, int[] postorder, &NBSP;INT&NBSP;POSTSTART,&NBSP;INT&NBSP;POSTEND,&NBSP;MAP<INTEGER,&NBSP;INTEGER>&NBSP;INORDERMAP) { if (instart > inend | | poststart > postend) return null; int Rootvalue = postorder[postend]; treenode root = new treenode (Rootvalue); int k = inordermap.get (Rootvalue); // From the post-order array, we know that last element is the root. we can Find the root in in-order array. then we can identify the left and right sub-trees of the root from in-order array. // !!! using the length of left sub-tree, we can identify left and right sub-trees in post-order array. recursively, we can build up the tree. int rightnodes = inend - k; // how many nodes on the right of k; Root.right = build (k+1, inend, postorder, postend - 1 - RIGHTNODES&NBSP;+&NBSP;1,&NBSP;POSTEND&NBSP;-&NBSP;1,&NBSP;INORDERMAP); root.left = build (instart, k-1, postorder, poststart, postend - &NBSP;1&NBSP;-&NBSP;RIGHTNODES,&NBSP;INORDERMAP); // becuase k is not the length, it it need to -(instart+1) to get the length return root; }}
[leetcode]106 Construct Binary Tree from inorder and Postorder traversal