1、来源: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.
2、思路,通过中序和后序来构建树:
- 后序的最后一个元素为当前根结点root;
- 在中序中,这个root左边为左子树,右边为右子树,递归就好了
3、代码:
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null || inorder.length == 0)
return null;
TreeNode root = biuld(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
return root;
}
private TreeNode biuld( int[] inorder, int start1, int end1,
int[] postorder, int start2, int end2) {
int root_val = postorder[end2];
TreeNode root = new TreeNode(root_val);
int i;
for(i = start1; i < end1; i++){
if(root_val == inorder[i])
break;
}
if(i - start1 > 0 ){
root.left = biuld( inorder, start1, i - 1, postorder, start2, start2 + i - 1 - start1);
}
if(end1 - i > 0 ){
root.right = biuld( inorder, i + 1, end1, postorder, end2 - end1 + i, end2 - 1);
}
return root;
}