public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(0, 0, inorder.length - 1, preorder, inorder);
}
private TreeNode helper(int preStart, int inStart, int inEnd,
int[] preorder, int[] inorder) {
if (preStart > preorder.length - 1|| inStart > inEnd) return null;
TreeNode root = new TreeNode(preorder[preStart]);
int inIndex = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == root.val) {
inIndex = i;
}
}
root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder);
root.right = helper(preStart + (inIndex - inStart) + 1, inIndex + 1, inEnd, preorder, inorder);
return root;
}
Given inorder and postorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
Note
类似之前的找法,不过这次我们需要从后面往前找 (inEnd should not larger than inStart)
Left
Right
postStart
postStart - (inStart - Index) - 1
postStart - 1
inStart
index - 1
inStart
inEnd
inEnd
index + 1
Code
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(inorder, inorder.length-1, 0, postorder, postorder.length-1);
}
private TreeNode buildTree(int[] inorder, int inStart, int inEnd, int[] postorder,
int postStart) {
if (postStart < 0 || inStart < inEnd)
return null;
//The last element in postorder is the root.
TreeNode root = new TreeNode(postorder[postStart]);
//find the index of the root from inorder. Iterating from the end.
int rIndex = inStart;
for (int i = inStart; i >= inEnd; i--) {
if (inorder[i] == postorder[postStart]) {
rIndex = i;
break;
}
}
//build right and left subtrees. Again, scanning from the end to find the sections.
root.right = buildTree(inorder, inStart, rIndex + 1, postorder, postStart-1);
root.left = buildTree(inorder, rIndex - 1, inEnd, postorder, postStart - (inStart - rIndex) -1);
return root;
}
Return any binary tree that matches the given preorder and postorder traversals.
Values in the traversals preandpost are distinct positive integers.
Example 1:
Input:
pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]
It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.