码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Day18|106.从中序与后序遍历序列构造二叉树


    一、106.从中序与后序遍历序列构造二叉树

    题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
    文章链接:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
    视频链接:https://www.bilibili.com/video/BV1vW4y1i7dn/?vd_source=721f65ae0501389782be0dcb48a2c421

    package com.eighteenday.tree;
    
    import java.util.Map;
    
    /**
     * 题目:根据一颗树的中序遍历与后序遍历构造二叉树
     * 注意:你可以假设树中没有重复的元素
     */
    public class BuildTree {
    
        Map<Integer,Integer> map;   //用map保存中序序列的数值对应位置
        public TreeNode buildTree(int[] inorder,int[] postorder){
            for (int i = 0; i < inorder.length; i++) {
                map.put(inorder[i],i);    //key:数组中的值,value:i
            }
    
            return findNode(inorder,0,inorder.length,postorder,0,postorder.length);
        }
    
        public TreeNode findNode(int[] inorder,int inBegin,int inEnd,int[] postorder,int postBegin,int postEnd){
            //参数里的范围都是左闭右开
            //大白话就是:判断传进的数组是否为空
            if (inBegin >= inEnd || postBegin >= postEnd){  //不满足左闭右开,说明没有元素,返回空树
                return null;
            }
            int rootIndex = map.get(postorder[postEnd-1]); //找到后序遍历的最后一个元素在中序遍历中的位置
            TreeNode root = new TreeNode(inorder[rootIndex]);//构造节点
            int lenOfLeft = rootIndex - inBegin ;  //保存中序左子树个数,用来确定后序数列的个数,tip:算的是大小(长度)
            //
            root.left = findNode(inorder,inBegin,rootIndex,postorder,postBegin,postBegin+lenOfLeft);
    
            root.right = findNode(inorder,rootIndex+1,inEnd,postorder,postBegin+lenOfLeft,postEnd);
    
            return root;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    ElasticSearch实战-RestAPI
    微信小程序入门讲解【超详细】
    线性表但是是Java中数组实用使用
    【移远QuecPython】EC800M物联网开发板的硬件PWM和PWM输出BUG
    wampserver简单配置
    Gitee使用方法
    React之一些函数或者方法的扩展
    爱上开源之DockerUI-xterm.js实现Web控制台
    【MySQL功法】第5话 · SQL单表查询
    边缘运算6大应用介绍
  • 原文地址:https://blog.csdn.net/My_Yes/article/details/133938343
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号