当前位置: 首页 > news >正文

LeetCode Hot100 105.从前序与中序遍历序列构造二叉树

题目:给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

代码

class Solution {
    private Map<Integer, Integer> indexMap;
    
    public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
        if (preorder_left > preorder_right) {
            return null;
        }
        // 前序遍历中的第一个节点就是根节点
        int preorder_root = preorder_left;
        // 在中序遍历中定位根节点
        int inorder_root = indexMap.get(preorder[preorder_root]);
        
        // 先把根节点建立出来
        TreeNode root = new TreeNode(preorder[preorder_root]);
        // 得到左子树中的节点数目
        int size_left_subtree = inorder_root - inorder_left;
        // 递归地构造左子树,并连接到根节点
        // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素
        root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1);
        // 递归地构造右子树,并连接到根节点
        // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素
        root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right);
        return root;
    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        // 构造哈希映射,帮助我们快速定位根节点
        indexMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            indexMap.put(inorder[i], i);
        }
        return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
    }
}

题目:给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

 中序 + 后序 构建 二叉树

class Solution {
    static int[] pos;
    static Map<Integer, Integer> map;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int posLen = postorder.length;
        int inLen = inorder.length;
        pos = postorder;
        map = new HashMap<>();
        for (int i = 0; i < inLen; i++) {
            map.put(inorder[i], i);
        }
        return buildTree(0, posLen - 1, 0, inLen - 1);
    }


    public TreeNode buildTree(int inL, int inR, int posL, int posR) {
        if (posL > posR || inL > inR) {
            return null;
        }
        int val = pos[posR];
        TreeNode root = new TreeNode(val);
        int piv = map.get(val);
        int sizeL = piv - inL;
        int posM = posL + sizeL - 1;
        root.left = buildTree(inL, piv - 1, posL, posM);
        root.right = buildTree(piv + 1, inR, posM + 1, posR - 1);
        return root;
    }
}
// 代码同 中序+先序构建二叉树,只需要把先序的地方改成后序
class Solution {
    private Map<Integer, Integer> indexMap;
    
    public TreeNode myBuildTree(int[] postorder, int[] inorder, int postorder_left, int postorder_right, int inorder_left, int inorder_right) {
        if (postorder_left > postorder_right || inorder_left > inorder_right) {
            return null;
        }
        // 后序遍历中的最后一个节点就是根节点
        int postorder_root = postorder_right;
        int val = postorder[postorder_root];
        // 在中序遍历中定位根节点
        int inorder_root = indexMap.get(val);
        // 先把根节点建立出来
        TreeNode root = new TreeNode(postorder[postorder_root]);
        // 得到左子树中的节点数目
        int size_left_subtree = inorder_root - inorder_left;
        
        root.left = myBuildTree(postorder, inorder, postorder_left, postorder_left + size_left_subtree - 1, inorder_left, inorder_root - 1);
        root.right = myBuildTree(postorder, inorder, postorder_left + size_left_subtree, postorder_right - 1, inorder_root + 1, inorder_right);
        
        return root;
    }

    public TreeNode buildTree(int[] inorder, int[] postorder ) {
        int n = postorder.length;
        // 构造哈希映射,帮助我们快速定位根节点
        indexMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            indexMap.put(inorder[i], i);
        }
        return myBuildTree(postorder, inorder, 0, n - 1, 0, n - 1);
    }
    
}

注意:如果人家给好的函数,你把形参数位置调换了,会一直报栈溢出StackOverFlow错误!!!

相关文章:

  • Django同时连接多种数据库
  • Python 测试框架 Pytest 的入门
  • 如何将本地websocket发布至公网并实现远程访问?
  • 十大排序之计数排序、桶排序、基数排序(详解)
  • Android YUV存储方式
  • 百度AI布局:从财报看百度的核心竞争力和未来发展方向
  • 如何找出excel中两列数据中不同的值(IF函数的用法)
  • 分布式事务seata的AT模式介绍
  • 【点云surface】Poisson表面重建
  • 【opencv】计算机视觉:实时目标追踪
  • 竞赛项目 车位识别车道线检测 - python opencv
  • 【OpenCV实现图像:使用OpenCV生成拼图效果】
  • Springboot websocket前端无法访问到,Websocket因AOP代理 前端无法请求到
  • 五种多目标优化算法(MOJS、NSGA3、MOGWO、NSWOA、MOPSO)求解微电网多目标优化调度(MATLAB代码)
  • Tomcat实现WebSocket即时通讯 Java实现WebSocket的两种方式
  • GEE:梯度提升树(Gradient Boosting Tree)分类教程(样本制作、特征添加、训练、精度、参数优化、贡献度、统计面积)
  • letcode::数组中的第k个最大元素
  • 软件设计先进性之虚拟化技术的应用
  • 三、Keil安装芯片包、下载固件库、建立STM32工程模板
  • Ceres使用
  • 上海现有超12.3万名注册护士,本科及以上学历占一半
  • 普京提议无条件重启俄乌谈判,外交部:我们支持一切致力于和平的努力
  • 尹锡悦涉嫌发动内乱案举行第三次庭审
  • 十三届全国政协经济委员会副主任张效廉被决定逮捕
  • 俄总统新闻秘书:普京提议谈判表明俄寻求和平解决方案意愿
  • “春申阡陌”漆画展:将传统漆艺融入现代创作