输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
思路:我们知道,如果只由前序遍历或者中序遍历是无法唯一确定一棵二叉树,除非你给的前序遍历或者中序遍历包括了null节点。而通过观察,我们可以发现,前序遍历拿到的根放置中序遍历,根的左区间和右区间分别对应当前根的左子树和右子树。我们递归调用即可完成这棵二叉树的重构。具体见代码;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
public TreeNode build(int[] preorder,int prestar,int preend,int[] inorder,int instar,int inend){
// 索引不合法 返回null
if(prestar > preend) return null;
// 当前先序遍历的节点就是根节点
TreeNode root = new TreeNode(preorder[prestar]);
// 记录当前根在中序遍历的位置
int in_root = 0;
// 这步也可以用HashMap来做,即提前存好中序遍历的节点对应的索引
for(int i = 0;i < inorder.length;i++){
if(inorder[i] == preorder[prestar]){
in_root = i;
}
}
// 拿到左子树的节点个数
int left_size = in_root - instar;
// 递归,分别是相对于先序遍历的左子树,中序遍历的左子树
// 以及相对于先序遍历的右子树,中序遍历的右子树
root.left = build(preorder,prestar+1,prestar+left_size,inorder,instar,in_root-1);
root.right = build(preorder,prestar+left_size+1,preend,inorder,in_root+1,inend);
return root;
}
}