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

Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树 立即下载

链接:

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

大意:

给定二叉树的前序遍历pre和中序遍历in(pre和in都是整型数组),构造该二叉树。例子:

思路:

每一颗树(或子树),前序遍历的第一个节点为根节点,找到根节点在中序遍历的位置(记为s吧),则在中序遍历中,s左边的元素都是根节点的左子树,s右边的元素都是根节点的右子树。

即记当前树的先序遍历序列在pre中的范围是[s1,e1],中序遍历序列在in中的范围是[s2,e2]。则对于找到的树(根节点值肯定是pre[s1],由于先序遍历的第一个值为根节点),s为pre[s1]在in中的位置,则根节点左子树在pre的范围是[s1 + 1, s1+ 1 + s - s1],根节点左子树在in中的范围是[s2, s - 1];根节点右子树在pre中的范围是[s1 + 1 + s - s1 + 1, e1],根节点右子树在in中的范围是[s + 1, e2]

之后便是递归构造树。

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] pre, int[] in) {
        if (pre.length == 0)
            return null;
        TreeNode root = buildTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
        return root;
    }
    public TreeNode buildTree(int[] pre, int s1, int e1, int[] in, int s2, int e2) {
        if (s1 > e1)
            return null;
        // 找到当前根节点在in中的位置
        TreeNode root = new TreeNode(pre[s1]);
        int s = s2, count = 0; // s记录根节点在in中的位置  count记录根的左子树有多少个节点
        while (s <= e2) {
            if (in[s] == pre[s1])
                break;
            s++;
            count++;
        }
        root.left = buildTree(pre, s1 + 1, s1 + count, in, s2, s - 1);
        root.right = buildTree(pre, s1 + count + 1, e1, in, s + 1, e2);
        return root;
    }
}

结果:

结论:

提交之前,我还是很相信这个方法的胜率的....  奈何事实在眼前,CAI(稍后优化) 

改进:

思考了一下,耗时较多的应该是在in中查找根节点的位置,是一个O(n)的操作。由于题目规定了每个节点的值都是不同的,于是可以使用一个map存储in中各个值与其在in中对应位置的映射,这时查找根节点在in中的时间复杂度变为O(1)了。改进代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] pre, int[] in) {
        if (pre.length == 0)
            return null;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < in.length;i++) {
            map.put(in[i], i);
        }
        TreeNode root = buildTree(pre, 0, pre.length - 1, in, 0, in.length - 1, map);
        return root;
    }
    public TreeNode buildTree(int[] pre, int s1, int e1, int[] in, int s2, int e2, Map<Integer, Integer> map) {
        if (s1 > e1)
            return null;
        // 找到当前根节点在in中的位置
        TreeNode root = new TreeNode(pre[s1]);
        int s = map.get(pre[s1]), count = s - s2; //s记录根节点在in中的位置  count记录根的左子树有多少个节点
        root.left = buildTree(pre, s1 + 1, s1 + count, in, s2, s - 1, map);
        root.right = buildTree(pre, s1 + count + 1, e1, in, s + 1, e2, map);
        return root;
    }
}

 

 

解决用VScode写中文注释,Vivado打开之后是乱码的方法 这样就解决了中文注释乱码的问题,此外如果使用notepad++写verilog代码的话直接在notepad++上面将编码格式改为ANSI格式即可,更加方便快捷,所以此方法仅针对习惯用VScode写Verilog的同学,如果有其他好办法的话欢迎评论区补充。首先,VScode中的中文默认编码格式是UTF-8,然后Vivado的编码格式是vivado 默认的应该是ANSI编码格式,刚开始我先按照网上说明的,将VScode中的编码格式改为GBK格式(点击右下角->点击通过编码保存)。 阅读详情

相关推荐

R 分析裂区试验设计

裂区试验有主区部分和副区部分, 因而有主区部分误差(误差 a,简记为Ea )和副区部分误差(误差b,简记为Eb) 分别用于测验主区处理以及副区处理和主、副互作的显著性。对同一个二因素试验资料作自由度和平方和的分解,则可发现随机区组的误差项自由度和平方和分别为DFe、SSe,而裂区设计有两个误差项,其自由度和平方和分别为DFEa,DFEb, SSEa, SSEb。而区组,处理效应等各个变异项目的自由

samhuairen的专栏 8940

105. Construct Binary Tree from Preorder and Inorder Traversal

题目描述(中等难度) 根据二叉树的先序遍历和中序遍历还原二叉树。 解法一 递归 先序遍历的顺序是根节点,左子树,右子树。中序遍历的顺序是左子树,根节点,右子树。 所以我们只需要根据先序遍历得到根节点,然后在中序遍历中找到根节点的位置,它的左边就是左子树的节点,右边就是右子树的节点。 生成左子树和右子树就可以递归的进行了。 比如上图的例子,我们来分析一下。 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] 首先根据 preorder 找到根节点是 3 然后根

猫敷雪 412

告别调参烦恼:用Simulink手把手实现自适应模糊PID温度控制(附模型下载)

本文详细介绍了如何使用Simulink实现自适应模糊PID温度控制,解决传统PID调参难题。通过模糊推理引擎动态调整PID参数,系统能自动适应不同工况,显著提升控制精度和响应速度。文章包含完整的建模步骤、参数优化技巧和工业应用扩展,并附赠可下载模型,帮助工程师快速掌握这一智能控制技术。

weixin_30872499的博客 160

LeetCode105: 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,15,20,7] Return the following binary tree: 3 / \

凌凌岛的博客 384

深入解析璞致PZ-ZU47DR RFSoC核心板:硬件架构与多通道ADC/DAC设计实战

本文深入解析璞致PZ-ZU47DR RFSoC核心板的硬件架构与多通道ADC/DAC设计实战。作为Xilinx Zynq UltraScale+ RFSoC系列的代表产品,PZ-ZU47DR集成了8通道5GSPS ADC和8通道9.85GSPS DAC,支持直接射频采样,大幅简化射频链路设计。文章详细介绍了其技术优势、设计挑战及实战技巧,为5G小基站、毫米波等应用场景提供高效解决方案。

meat5的博客 863

【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,15,20,7] Return the following binary tree: 3 ...

韩旭051的博客 703

【遥感数字图像处理(朱文泉)】各章博文链接汇总及思维导图

遥感数字图像处理(北京师范大学朱文泉教授)课程各章博文笔记汇总,及各章节思维导图

甜子yu的博客 2642

经典:Construct Binary Tree from Preorder and Inorder Traversal:由先序中序构建二叉树

Given preorder and inorder traversal of a tree, construct the binary tree. 思路:手动模拟的时候,也是递归的重复。所以成序也是递归。为了减少重复时间,实现计算好中序中各个元素对应的下标位置。难点在于计算右子树时,需要计算偏移量。 这道题是必须会的一道题,非常非常的经典! /** * Definition for

小雄鹿的博客 274

ansys非线性分析中接触类型

不分离(No Separation)‌:法向始终保持接触不分离,切向允许自由滑动,属于线性接触,适合需要法向约束、允许微小切向位移的场景,比如过盈配合的简化模拟。有摩擦(Frictional)‌:法向允许分离,切向根据用户设置的摩擦系数计算摩擦力,是工程中最符合实际机械接触的类型,绝大多数装配体分析都选择该类型。无摩擦(Frictionless)‌:法向允许接触面分离,切向无摩擦、允许自由滑动,适合模拟理想润滑的光滑接触面,图中当前选择的就是该类型。非线性接触(需要多次迭代,属于真正的非线性接触行为)‌。

qq_15963745的博客 82

【实战项目开发技术分享】ROS1.0、ROS 2.0和CyberRT开源通信框架对比

随着机器人和自动驾驶技术的飞速发展,开源通信框架在机器人、自动驾驶行业扮演着重要的角色。ROS1.0是一个广泛应用于机器人领域的开源框架,ROS 2.0是其改进版本,而CyberRT是Baidu Apollo自动驾驶平台的通信框架。ROS和ROS 2.0都注重灵活性和共享性,适用于各种机器人应用。CyberRT则专注于实时性和高性能,适用于自动驾驶等对实时性要求较高的场景。选择适合的框架取决于具体应用需求和开发者的偏好。

xiewf的博客 2万+

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

以下是爆栈写法 当前测试过不掉了(以前貌似可以 看到discus里有好多和我一样的写法 原因就是传递函数的过程中复制消耗了栈空间 同时时间也不乐观的大量提升 所以告诉我们一个道理。能用辅助函数就用辅助函数来操作上下界,直接写太ugly 维护性差 而且内存会爆炸。 /* * @lc app=leetcode id=105 lang=cpp * * [105] Construct Binary...

OCEANtroye的博客 443

[Dify] 实现“多知识库切换”功能的最佳实践

知识库切换是提升AI问答系统精准度的关键技术。本文介绍了Dify平台实现多知识库切换的三种方式:静态配置、元数据过滤和动态切换,分析了各自的优缺点。最佳实践建议包括:合理分库设计、意图分类、主库优先检索等。文章还提供了示例工作流程和注意事项,帮助构建更智能、精准的知识驱动型问答系统。通过合理切换知识库,可显著提升回答相关性和系统性能。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 449

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversa

crazy_jack 8975

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的前序和中序遍历,重建这棵二叉树。

哈哈的个人专栏 1690

visio安装包

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

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

原题链接:

Ber03的博客 240

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. 从前序遍历数组和中序遍历数组 ,得到二叉树。

Practice when no one is watching... 909

leetcode105 - 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. 这道题给出先序和中序 要求画出所给的唯一的二叉树。只需要顺序遍历先序数组,然后再把该元素放入到中序中,将中序数组分为左右两边就行了。 /** * Definition for a binary tree node. * st

i_actor的博客 229

Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal

Leetcode105. 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...

magic_jiayu的博客 288

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

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

i逆天耗子 635

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.  原题链接:https://leetcode.com/problems/construct-binary-tree

WangT443的博客 730

【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目来源:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given p

ZeeCoder 2421

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. 根据先序和中序遍历还原二叉树。 思路很简单,先序中的第一个点必然为root,所以只要以先序的第一个元素在中序中的

无名山丘,崛起成峰 1072

leetcode105. Construct Binary Tree from Preorder and Inorder Traversal

今天刷LeetCode,一个很简单的题目,我的提交如下 class Solution { public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { return bt(preorder, inorder, 0, preorder.size()-...

weixin_44021477的博客 136

[JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal

根据前序遍历和中序遍历,构建二叉树。假设树中不存在相同的节点

fumier的专栏 1045
上一篇: leetcode----103. Binary Tree Zigzag Level Order Traversal
下一篇: leetcode----106. Construct Binary Tree from Inorder and Postorder Traversal
成程晨
博客等级 码龄9年 201粉丝 148原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值