題目:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
題解:
這道題跟pre+in一樣的方法做,只不過找左子樹右子樹的位置不同而已。
1 / \ 2 3 / \ / \ 4 5 6 7
對於上圖的樹來說,
index: 0 1 2 3 4 5 6
中序遍歷為: 4 2 5 1 6 3 7
後續遍歷為: 4 5 2 6 7 3 1
為了清晰表示,我給節點上了顏色,紅色是根節點,藍色為左子樹,綠色為右子樹。
可以發現的規律是:
1. 中序遍歷中根節點是左子樹右子樹的分割點。
2. 後續遍歷的最後一個節點為根節點。
同樣根據中序遍歷找到根節點的位置,然後順勢計算出左子樹串的長度。在後序遍歷中分割出左子樹串和右子樹串,遞迴的建立左子樹和右子樹。
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
}
public TreeNode buildTree(int[] in, int inStart, int inEnd, int[] post, int postStart, int postEnd){
if(inStart > inEnd || postStart > postEnd){
return null;
}
int rootVal = post[postEnd];
int rootIndex = 0;
for(int i = inStart; i <= inEnd; i++){
if(in[i] == rootVal){
rootIndex = i;
break;
}
}
int len = rootIndex - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = buildTree(in, inStart, rootIndex-1, post, postStart, postStart+len-1);
root.right = buildTree(in, rootIndex+1, inEnd, post, postStart+len, postEnd-1);
return root;
}
return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
}
public TreeNode buildTree(int[] in, int inStart, int inEnd, int[] post, int postStart, int postEnd){
if(inStart > inEnd || postStart > postEnd){
return null;
}
int rootVal = post[postEnd];
int rootIndex = 0;
for(int i = inStart; i <= inEnd; i++){
if(in[i] == rootVal){
rootIndex = i;
break;
}
}
int len = rootIndex - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = buildTree(in, inStart, rootIndex-1, post, postStart, postStart+len-1);
root.right = buildTree(in, rootIndex+1, inEnd, post, postStart+len, postEnd-1);
return root;
}