Believing Process 剑指Offer7.重建二叉树

题干:输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。

假设输入的前序遍历和中序遍历的结果中都不含重复的数字。

示例:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]

class Solution {
    int[] preorder;
    HashMap<Integer, Integer> map = new HashMap<>();
    // 前序遍历 preorder: 根 -- 左 -- 右   第一个肯定是根节点
    // 中序遍历 inorder: 左 -- 根 -- 右
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        for(int i = 0; i < inorder.length; i++){
            map.put(inorder[i], i);
        }
        return rebuild(0, 0, inorder.length - 1);  
    }

    // pre_root_index : 根节点 在 前序遍历中的下标
    // in_left_index: 该节点在中序遍历中的左边界
    // in_right_index: 该节点在中序遍历中的右边界
    public TreeNode rebuild(int pre_root_index, int in_left_index, int in_right_index){
       if(in_left_index > in_right_index)  return null;
       // 根节点在中序遍历中的位置:in_root_index
       int in_root_index = map.get(preorder[pre_root_index]);
       // 创建一个根节点
       TreeNode node = new TreeNode(preorder[pre_root_index]);
       // 寻找node的左节点: 
       // 在前序遍历中的位置就是  根节点的下标 + 1(右边一个单位)
       // 在中序遍历中的位置就是: 1. 左边界不变,2. 右边界就是根节点的左边一个单位 in_root_index - 1
       node.left = rebuild(pre_root_index + 1, in_left_index, in_root_index - 1);
       // 寻找node的右节点: 
       // 在前序遍历中的位置就是  根节点的下标 + 左子树长度 + 1
       // 在中序遍历中的位置就是: 1. 左边界在根节点的右边一个单位  in_root_index + 1, 2. 右边界不变
       node.right = rebuild(pre_root_index + in_root_index - in_left_index + 1, in_root_index + 1, in_right_index);
       return node;
    }
}

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return build(preorder,0, preorder.length, inorder,0, inorder.length);
    }
    public TreeNode build(int[]preorder,int p_start,int p_end,int[] inorder, int i_start,int i_end){
        if(p_start==p_end) return null;
        int root_val = preorder[p_start];
        TreeNode root = new TreeNode(root_val);
        //在中序遍历中找到根节点的位置
        int i_root_index = 0;
        for(int i=i_start;i<i_end;i++){
            if(root_val==inorder[i]) {
                i_root_index = i;
                break;
            }
        }
        int leftNum = i_root_index-i_start;
        //递归构造左子树
        root.left = build(preorder,p_start+1,p_start+1+leftNum,inorder,i_start,i_root_index);
        //递归构造右子树
        root.right = build(preorder,p_start+1+leftNum, p_end, inorder,i_root_index+1,i_end);
        return root;
    }
}

 

        理解题目的意思,是要我们根据前序和中序遍历重建二叉树,那么我们能首先想到的是前序遍历的第一个节点就是二叉树的根节点,然后从中序遍历中可以根据根节点,找到左右子树,递归即可完成二叉树的重建。

        首先是用一个键值对存储中序遍历中的值以及其对应的下标,方便后续我们可以根据当前节点在前序遍历中的下标从而找到当前节点在中序遍历的下标,这样的一个操作。之后就可以开始递归了。

        递归三要素,1.终止条件   2.此次递归所需要进行的操作  3.返回值。 

        根据上述三要素展开递归,首先终止条件就是当前节点的左边界的下标大于当前节点的右边界的下标;此次递归所要进行的操作就是先根据键值对找到当前节点在中序遍历中的下标,然后新建节点,递归去找当前节点的左右子节点;返回值就是当前这个节点。

        PS:左子树的长度就是在中序遍历中当前节点的下标 - 左边界下标加1。

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值