【LeetCode笔记】105. 从前序与中序遍历序列构造二叉树(Java、递归、二叉树、哈希表)

题目描述

  • 这题主要是考察前序、后序的性质,以及相互间的关系
    在这里插入图片描述

代码 & 思路

  • 前序:根 - 左 - 右; 中序:左 - 根 - 右,那么用前序数组的首位(即根)的值到中序数组查找对应位置,就可以由此得到左右子树的结点数。
  • 递归进行,每次构造当前根结点,然后开启左、右子树的递归函数即可。
  • 用一次循环来构建<根结点val,中序index>的哈希表,就不用每次都循环查找一次根节点index。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    /**
        如何递归?
        题目要求:给定前序和后序,构造树。
        既然是前序和后序,那么数组中左子树和右子树一定可以分离成两个连续子数组
        冲!
    */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        // 根据preorder中的一个值,就能找到在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){
        // 找到root.val在inorder中的位置
        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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值