Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder == null || inorder.length == 0 || postorder == null || postorder.length == 0) {
return null;
}
if (inorder.length == 1) {
return new TreeNode(inorder[0]);
}
TreeNode node = new TreeNode(postorder[postorder.length - 1]);
int index = indexOf(inorder, node.val);
if (index >= 1) {
node.left = buildTree(Arrays.copyOfRange(inorder, 0, index), Arrays.copyOfRange(postorder, 0, index));
}
if (postorder.length -1 >= index)
node.right = buildTree(Arrays.copyOfRange(inorder, index + 1, inorder.length), Arrays.copyOfRange(postorder, index, postorder.length - 1));
return node;
}
public int indexOf(int[] arr, int val) {
if (arr == null || arr.length == 0) return -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == val) {
return i;
}
}
return -1;
}
}
本文详细介绍了如何通过给定的二叉树中序和后序遍历序列,构建原始二叉树的过程。通过递归算法实现,确保在不重复元素的情况下准确重建树结构。
252

被折叠的 条评论
为什么被折叠?



