题目描述
- 这题主要是考察前序、后序的性质,以及相互间的关系
代码 & 思路
- 前序:根 - 左 - 右; 中序:左 - 根 - 右,那么用前序数组的首位(即根)的值到中序数组查找对应位置,就可以由此得到左右子树的结点数。
- 递归进行,每次构造当前根结点,然后开启左、右子树的递归函数即可。
- 用一次循环来构建<根结点val,中序index>的哈希表,就不用每次都循环查找一次根节点index。
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer,Integer> map = new HashMap();
for(int i=0;i < inorder.length;i++){
map.put(inorder[i],i);
}
return toBuildTree(new TreeNode(),preorder,0,preorder.length-1,inorder,0,inorder.length-1,map);
}
TreeNode toBuildTree(TreeNode root, int[] preorder, int pL, int pR, int[] inorder, int iL, int iR,
Map<Integer,Integer> map){
int rootIndex = map.get(preorder[pL]);
root = new TreeNode(inorder[rootIndex]);
int leftNum = rootIndex - iL;
int rightNum = iR - rootIndex;
if(leftNum > 0){
root.left = toBuildTree(root.left, preorder, pL + 1, pL + leftNum,
inorder, iL, rootIndex - 1, map);
}
if(rightNum > 0){
root.right = toBuildTree(root.right, preorder, pL + leftNum + 1, pR,
inorder, rootIndex + 1, iR, map);
}
return root;
}
}
class Solution {
Map<Integer, Integer> hashmap = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for(int i = 0; i < inorder.length; i++) {
hashmap.put(inorder[i], i);
}
return toBuildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
public TreeNode toBuildTree(int[] preorder, int pL, int pR, int[] inorder, int iL, int iR) {
int rootIndex = hashmap.get(preorder[pL]);
TreeNode root = new TreeNode(preorder[pL]);
int leftNum = rootIndex - iL;
int rightNum = iR - rootIndex;
if(leftNum > 0) {
root.left = toBuildTree(preorder, pL + 1, pL + leftNum, inorder, iL, rootIndex - 1);
}
if(rightNum > 0) {
root.right = toBuildTree(preorder, pL + leftNum + 1, pR, inorder, rootIndex + 1, iR);
}
return root;
}
}