输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
限制:
0 <= 节点个数 <= 5000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
首先我们先复习下二叉树的前序,中序,后序遍历方式
比如下面的二叉树
前序(头左右):preorder = [A,B,D,E,C,F,G]
中序(左头右):inorder = [D,B,E,A,F,C,G]
后序(左右头):postorder=[D,E,B,F,G,C,A]
已知前序,中序求二叉树
1.首先先根据前序的首字母得到二叉树的头结点 A
2.计算出头结点在中序中的位置,头结点左边是左子树[D,B,E],右边是右子树[F,C,G]
3.左子树 的前序是[BDE],中序是[D,B,E],接下的思路这又与1,2步重复,所以根据递归思想得出算法
二叉树定义
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
构建二叉树部分
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null && inorder==null){
return null;
}
//构建map方便查找位置,和避免重复
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0;i < inorder.length;i++){
map.put(inorder[i],i);
}
return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, map);
}
private TreeNode build(int[] p, int pi, int pj, int[] n, int ni, int nj, Map<Integer, Integer> map) {
if (pi > pj) {
return null;
}
TreeNode head = new TreeNode(p[pi]);
//得到头结点位置
int index = map.get(p[pi]);
//构建左子树
head.left = build(p, pi + 1, pi + index - ni, n, ni, index - 1, map);
//构建右子树
head.right = build(p, pi + index - ni + 1, pj, n, index + 1, ni, map);
return head;
}