根据前序遍历和中序遍历树构造二叉树.
样例
给出中序遍历:[1,2,3]
和前序遍历:[2,1,3]
. 返回如下的树:
2
/ \
1 3
注意事项
你可以假设树中不存在相同数值的节点
解题思路:
1
/ \
2 3
/ \ / \
4 5 6 7
对于上图的树来说,
index: 0 1 2 3 4 5 6
先序遍历为: 1 2 4 5 3 6 7
中序遍历为: 4 2 5 1 6 3 7
为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
可以发现的规律是:
1. 先序遍历的从左数第一个为整棵树的根节点。
2. 中序遍历中根节点是左子树右子树的分割点。
再看这个树的左子树:
先序遍历为: 2 4 5
中序遍历为: 4 2 5
依然可以套用上面发现的规律。
右子树:
先序遍历为: 3 6 7
中序遍历为: 6 3 7
也是可以套用上面的规律的。
所以这道题可以用递归的方法解决。
具体解决方法是:
通过先序遍历找到第一个点作为根节点,在中序遍历中找到根节点并记录index。
因为中序遍历中根节点左边为左子树,所以可以记录左子树的长度并在先序遍历中依据这个长度找到左子树的区间,用同样方法可以找到右子树的区间。
递归的建立好左子树和右子树就好。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param inorder: A list of integers that inorder traversal of a tree
* @param postorder: A list of integers that postorder traversal of a tree
* @return: Root of a tree
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
// write your code here
return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1);
}
public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd){
if(inStart>inEnd || preStart>preEnd)
return null;
int rootVal = preorder[preStart];//先序遍历找到第一个点作为根节点
int rootIndex = 0; //在中序遍历中找到根节点并记录index
for(int i=inStart ; i<=inEnd ; i++){
if(inorder[i] == rootVal){
rootIndex = i;
break;
}
}
int len = rootIndex - inStart; //左子树的长度
TreeNode root = new TreeNode(rootVal);
root.left = buildTree(preorder,preStart+1,preStart+len,inorder,inStart,rootIndex-1);
root.right = buildTree(preorder,preStart+len+1,preEnd,inorder,rootIndex+1,inEnd);
return root;
}
}
优化:由于每次递归都需要在中序遍历数组中寻找根节点的位置索引,想到如果用hashmap集合存放inorder数组,查找效率更高
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param inorder: A list of integers that inorder traversal of a tree
* @param postorder: A list of integers that postorder traversal of a tree
* @return: Root of a tree
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
// write your code here
Map<Integer,Integer> map= new HashMap<>();//key为inorder数组中的值,value为对应的数组索引
for(int i=0 ; i<inorder.length ; i++)
map.put(inorder[i],i);
return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1, map);
}
public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer,Integer> map){
if(inStart>inEnd || preStart>preEnd)
return null;
int rootVal = preorder[preStart];//先序遍历找到第一个点作为根节点
int rootIndex = map.get(rootVal); //在中序遍历中找到根节点并记录index
int len = rootIndex - inStart; //左子树的长度
TreeNode root = new TreeNode(rootVal);
root.left = buildTree(preorder,preStart+1,preStart+len,inorder,inStart,rootIndex-1,map);
root.right = buildTree(preorder,preStart+len+1,preEnd,inorder,rootIndex+1,inEnd,map);
return root;
}
}