LeetCode题解-105-Construct Binary Tree from Preorder and Inorder Traversal

如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤 如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤。 阅读详情

原题

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:You may assume that duplicates do not exist in the tree. 

原题链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/


解题思路

前序遍历与中序遍历的结构为:

前序遍历:[ 根节点, 左子树节点X个, 右子树节点Y个 ];

中序遍历:[ 左子树节点X个, 根节点, 右子树节点Y个 ];

因此,解题思路为:

1、前序遍历的第一个节点为根节点;

2、中序遍历中可找到根节点,在根节点之前的为左子树的节点,在根节点之后的为右子树的节点;

3、知道了左子树的节点个数与右子树的节点个数后,可以找到前序遍历中左子树节点与右子树节点;

4、递归终止条件为前序遍历仅有一个节点,此时该节点即为根节点。


代码1

该实现参考: https://discuss.leetcode.com/topic/3695/my-accepted-java-solution/2,比较简洁。

public class Solution105_recursive {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
       return buildTreeHelper(0, 0, inorder.length - 1, preorder, inorder);
    }

    private TreeNode buildTreeHelper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) {
        if (preStart > preorder.length - 1 || inStart > inEnd)
            return  null;

        TreeNode root = new TreeNode(preorder[preStart]);
        int inIndex = 0;
        for (int i = inStart; i <= inEnd; i++){
            if (inorder[i] == root.val)
                inIndex = i;
        }

        root.left = buildTreeHelper(preStart + 1, inStart, inIndex - 1, preorder, inorder);
        root.right = buildTreeHelper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder);
        return root;
    }
}


代码2

为自己最初的解法,

public class Solution105_recursive {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0)
            return null;
        else if (preorder.length == 1)
            return new TreeNode(preorder[0]);

        int rootIndexOfInorder = getIndexFromArray(preorder[0], inorder);
        int[] leftInOrder = getSubArray(inorder, 0, rootIndexOfInorder - 1);
        int[] rightInOrder =  getSubArray(inorder, rootIndexOfInorder + 1, inorder.length - 1);
        int[] leftPreOrder = getSubArray(preorder, 1, leftInOrder.length);
        int[] rightPreOrder = getSubArray(preorder, leftInOrder.length + 1, leftInOrder.length + rightInOrder.length);

        TreeNode root = new TreeNode(preorder[0]);
        root.left = buildTree(leftPreOrder, leftInOrder);
        root.right = buildTree(rightPreOrder, rightInOrder);

        return root;
    }

    private int[] getSubArray(int[] array, int start, int end) {
        if (start > end)
            return new int[0];

        int[] subArray  = new int[end - start + 1];
        int indexOfSubArray = 0, indexOfArray = start;
        while (indexOfArray != end)
            subArray[indexOfSubArray++] = array[indexOfArray++];
        subArray[indexOfSubArray] = array[indexOfArray];

        return subArray;
    }

    private int getIndexFromArray(int val, int[] array) {
        int index = 0;
        while (val != array[index++]);

        return --index;
    }

}
从零开始,手把手教你用RTL代码设计一个简单的8位CPU(附状态图与ALU设计) 本文详细介绍了如何从零开始设计一个简单的8位CPU,包括状态机设计、ALU实现与RTL编码实战。通过具体的指令集分析、状态图设计和Verilog代码示例,帮助读者深入理解CPU工作原理,并掌握RTL代码编写技巧。特别适合计算机系统学习者和硬件设计爱好者。 阅读详情

相关推荐

白话详细解读(七)----- CBAM:Convolutional Block Attention Module

一、CBAM简介 论文链接:https://x.sci-hub.org.cn/scholar?hl=zh-TW&as_sdt=0%2C5&q=CBAM%3A+Convolutional+Block+Attention+Module&btnG=&oq=CB CBAM这篇论文是在2018年提出的,自提出之后,已经被引用上千次,可见其影响力。CBAM是一种专门为了卷积神经网络而设计的注意力模块,传统的卷积存在只关注局部信息,而往往忽略全局信息的弊端,以3*3卷积为例,滤波器有9个

奋斗の博客 1万+

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目。 1、确定树的根节点。树根是当前树中所有元素在前序...

SmartSi 407

ESP32Servo库驱动SG90舵机:从入门到精准控制

本文详细介绍了如何使用ESP32Servo库驱动SG90舵机,从硬件连接到软件环境搭建,再到编写示例程序实现精准控制。内容涵盖库安装、基础驱动代码解析、舵机校准方法以及平滑运动控制,帮助开发者快速掌握ESP32控制舵机的核心技能,并提供了电源管理、信号抗干扰等实用避坑指南。

ik678901的博客 908

jQuery中的索引inArray与获取index方法

(作者:老薛,撰写时间:2019年5月27日) jQuery对象是我们学jQuery的基础,jQuery里面还有一个方法value。它可以在jQuery的索引inArray使用。 首先我们先看下面这张截图: 图1 我们也看到了图1里面的value方法在jQuery对象的使用,而下面我要说的是jQuery中查找数组中...

weixin_44550190的博客 1650

避坑指南:Livox Mid-360双雷达SLAM配置中,那些官方文档没说的细节(IP设置、话题分离与时间同步)

本文详细解析了Livox Mid-360双雷达SLAM配置中的关键优化点,包括IP设置、话题分离与时间同步等官方文档未提及的细节。通过实战案例和代码示例,帮助开发者解决点云错位、IMU数据混乱等问题,提升SLAM系统的稳定性和精度。特别针对网络架构设计、驱动修改和时间同步机制提供了专业指导。

weixin_29745609的博客 430

from pointnet2_ops import pointnet2_utils 安装过程

pointnet2_ops

goodgoodstudy___的博客 8371

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 题解

题目链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/?tab=Description

xiaoyi357的博客 390

时间序列分析:ARCH、GARCH、TGARCH与DCC-GARCH波动率模型(含代码)

关注TGARCH模型和DCC-GARCH的应用,通过在沪深300和中证500指数上应用TGARCH和DCC-GARCH模型,揭示这两个指数之间的条件异方差及动态相关性的变化规律。

celiaweiwei的博客 1万+

leetcode题解:第105题Construct Binary Tree from Preorder and Inorder Traversal

https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 文章目录解法一、递归代码复杂度分析解法二、迭代 解法一、递归 前序遍历的序列组成形式为:根, {左子树前序结果}, {右子树前序结果} 中序遍历的序列组成形式为:{左子树中序结果}, 根, {右子树中序结果} 把上面这两行写出来,递归解法就很容易想到了。 很明显,我们需要知道左子树的节点个数,才好获取到左子树的前序结果。由于树

chenf1999的博客 346

2024年全国大学生信息安全竞赛安徽省赛网络系统建设与运维赛项-网络构建真题

该网络工程方案主要包含基础配置、有线网络、无线网络、出口网络、运维配置和SDN网络六个部分。基础配置包括设备接口设置、SSH服务开启和SNMP功能配置。有线网络部分重点部署VLAN、MSTP、VRRP和OSPF协议,实现网络冗余和负载均衡。无线网络规划了AP点位、信号覆盖和用户接入策略,包括SSID配置、频段优化和安全认证。出口网络配置了NAT转换、VPN隧道和流量审计功能。运维方面搭建了监控平台实时监测设备状态。SDN网络部分通过控制器实现流量管控。整个方案实现了多业务隔离、高可用性网络架构和

weixin_42221934的博客 1180

Python爬虫实战:基于Scrapy与Playwright高效爬取人民网新闻数据

本文将详细介绍如何使用Python最新技术栈(Scrapy框架结合Playwright浏览器自动化工具)高效爬取人民网新闻数据。文章包含完整项目搭建、反反爬策略、数据解析存储以及性能优化等核心内容,提供可直接运行的代码示例,适合中高级Python开发者学习现代网络爬虫开发技术。python# 去除特殊空白字符。

2201_76125261的博客 1126

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal题解(python)

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal 分类:Tree & Recursion 难度:M 描述:给了一个树的先序遍历和中序遍历的结果,让把这歌二叉树给复现出来。 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Tree: 3 / \ 9 20 / \ 15 7 链接: Construct Binary T

Bourns_A的博客 373

Kimi Code国产AI编程助手安装使用详细教程

Kimi Code 开发者智能编程服务简介:Kimi Code 是专为开发者提供的 AI 编程辅助工具,基于旗舰模型 Kimi K3(2.8万亿参数,支持100万token上下文)。提供 CLI、VS Code 插件等形式,支持代码阅读、编辑等功能。用户可通过API Key接入第三方工具。安装简单(Windows/Mac/Linux一键脚本),需在官网生成API密钥并登录使用。注意账户需充值余额方可正常调用服务。

猿小白的博客 2143

leetcode---105. Construct Binary Tree from Preorder and Inorder Traversal C++题解

105. Construct Binary Tree from Preorder and Inorder Traversal 这道题剑指offer已经做过了,见重建二叉树 直接复制思路: C++。首先要知道一个结论,前序/后序+中序序列可以唯一确定一棵二叉树,所以自然而然可以用来建树。 看一下前序和中序有什么特点,前序1,2,4,7,3,5,6,8 ,中序4,7,2,1,5,3,8,6; 有...

bbkgl的博客 390

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal

其实这两道题目就是从二叉树的前序遍历(后序遍历)和中序遍历数组中恢复出二叉数即可。我们知道二叉树的三种遍历方法后本题就变得很简单了。 先序遍历: 若二叉树为空,则不进行任何操作:否则 1、访问根结点。 2、先序方式遍历左子树。 3、先序遍历右子树。 中序遍历: 若二叉树为空,则不进行任何操作

呜呜哈的博客 1241

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

leetcode题解(四十二):105. Construct Binary Tree from Preorder and Inorder Traversal

给出前序和中序遍历,构造二叉树 通过前序遍历得到根节点,中序遍历得到左右子树,递归下去就可以了 public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0, 0, inorder.length - 1, preorder, inorder); } public TreeNode helper(in...

微博@山椒鱼只想坐在地上生根发芽 270

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

[LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

题目来源:   https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析:   根据二叉树的中序和前序遍历结果,反推这个树,假设只有一个这样的树。 题目思路:   前序遍历的第一个树将中序遍历分成两半,左半部分是左子树,右半部分是右子树,递...

weixin_34304013的博客 114

105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 这道题是利用一棵树的先序遍历,和中序遍历重建出一颗树,LeetCode中有一道相似的题目:106. Const

我这知识都学杂了 410

【算法】【c++】【代码】105. Construct Binary Tree from Preorder and Inorder Traversal

经典题目,同理还有通过inorder和postorder构建binary tree Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/construct-binary-tree-fr

weixin_44420980的博客 216

LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder =[3,9,20,15,7] inorder = [9,3...

reigns的博客 169

LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal

对原有的前序数组进行切片(弹出第一个元素),类似106题中将后序数组进行resize。

叶卡捷琳堡的博客 173

LeetCode Top 100 Liked Questions 105. Construct Binary Tree from Preorder and Inorder Traversal (M)

welcome to my blog LeetCode Top 100 Liked Questions 105. Construct Binary Tree from Preorder and Inorder Traversal (Java版; Medium) 题目描述 Given preorder and inorder traversal of a tree, construct the bi...

littlehaes的博客 271

LeetCode-105从前序与中序遍历序列构造二叉树(Construct Binary Tree from Preorder and Inorder Traversal)

LeetCode-105 Given preorder and inorder traversal of a tree, construct the binary tree. Example input: preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] return: [3,9,20,null,null,15,7]  &nbsp...

V2beach的博客 305

【LeetCode】No.105. Construct Binary Tree from Preorder and Inorder Traversal -- Java Version

【LeetCode】No.105. Construct Binary Tree from Preorder and Inorder Traversal -- Java Version

汤姆lazy的CSDN博客 441

【算法】leetcode105. Construct Binary Tree from Preorder and Inorder Traversal从前序与中序遍历序列构造二叉树

这题给的是前序遍历和中序遍历。前序遍历的遍历顺序是根->左子树->右子树,中序遍历的遍历顺序是左子树->根->右子树。所以根据前序遍历找到根节点后,再从中序遍历中找到根的位置,从而知道左子树和右子树的大小,再根据大小,获得前序遍历的左子树和右子树,递归遍历这个过程。给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。是不变数组,用双指针。preorder 保证 为二叉树的前序遍历序列。

adsam123的博客 168

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:http...

负雪明烛 3819
上一篇: LeetCode题解-113-Path Sum II
下一篇: LeetCode题解-106-Construct Binary Tree from Inorder and Postorder Traversal
WangT443
博客等级 码龄12年 2粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值