栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

剑指 Offer 07. 重建二叉树

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

剑指 Offer 07. 重建二叉树

剑指 Offer 07. 重建二叉树

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

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

示例 1:

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

Input: preorder = [-1], inorder = [-1]
Output: [-1]
 

限制:

0 <= 节点个数 <= 5000

解法一:HashMap 找根

class Solution {
    int[] preorder;
    HashMap dic = new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) {
       this.preorder = preorder;
       for(int i = 0; i < inorder.length; i++){
           dic.put(inorder[i],i);
       }
       return recur(0,0,inorder.length - 1);
    }
    TreeNode recur(int root,int left,int right){
        if(left > right) return null;
        TreeNode node = new TreeNode(preorder[root]);
        int i = dic.get(preorder[root]);
        node.left = recur(root + 1, left,i - 1);
        node.right = recur(root + i - left + 1, i + 1,right);
        return node;
    }
}

解法二:基础 必会的

class Solution {
    public int getIndex(int[] arr ,int target){
        for(int i = 0; i < arr.length; i++ ){
            if(target == arr[i])
            return i;
        }
        return 0;
    }
  
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        //做特殊判断
        if(preorder == null || inorder == null || preorder.length == 0
         || inorder.length == 0 || preorder.length != inorder.length)
         return null;
         //创建根节点
         TreeNode root = new  TreeNode(preorder[0]);
         int rootVal = getIndex(inorder,root.val);
         //找到左子树的前序和中序
         int[] leftPer = Arrays.copyOfRange(preorder,1,rootVal + 1);
         int[] leftIno = Arrays.copyOfRange(inorder,0,rootVal);
         //找到右子树的前序和中序
         int[] rightPer = Arrays.copyOfRange(preorder,rootVal + 1,preorder.length);
         int[] rightIno = Arrays.copyOfRange(inorder,rootVal + 1,inorder.length);
         //递归遍历
         root.left = buildTree(leftPer,leftIno);
         root.right = buildTree(rightPer,rightIno);
         return root;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号