Given Inorder and Postorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
Or the reconstruction of the tree with the previous question compared to the sequence of ideas or the same code as follows:
public class Solution {public TreeNode buildtree (int[] inorder, int[] postorder) { if (postorder.length==0& &inorder.length==0) {return null;} TreeNode root=new TreeNode (postorder[postorder.length-1]); int begindex=0;for (int i=0;i<inorder.length;i++) {if ( Inorder[i]==postorder[postorder.length-1]) {begindex=i;break;}} int leftlen=begindex; int rightlen=inorder.length-begindex-1; Int[] Leftpost=new Int[leftlen]; Int[] leftino=new int[leftlen];for (int i=0;i<begindex;i++) {leftpost[i]=postorder[i];leftino[i]=inorder[i];} Root.left=buildtree (Leftino, leftpost); int[] Rightpost=new int[rightlen];int[] rightino=new int[rightlen];for (int i= 0;i<rightlen;i++) {rightpost[i]=postorder[i+begindex];rightino[i]=inorder[i+begindex+1];} Root.right=buildtree (Rightino, rightpost); return root; }}
Construct Binary Tree from inorder and Postorder traversal