leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal(前序和中序遍历重建二叉树)

AI风控系统中的实时决策架构:架构师的关键设计 当用户点击「确认支付」时,一场毫秒级的“欺诈攻防战”正在后台悄然打响:系统需要在100ms内完成用户行为特征提取→欺诈模型推理→规则引擎校验→最终决策返回。这就是AI风控实时决策架构的核心——用技术对抗“实时化的欺诈手段”。本文将以「餐厅智能点餐系统」为类比,拆解实时风控架构的每一层逻辑:从“如何快速拿到用户的实时行为特征”(像服务员实时收集顾客的忌口),到“如何让模型和规则协同决策”(像厨师长结合菜谱和经验判断菜能不能做),再到“如何保证系统扛住大促的高并发”(像餐厅加桌子应对客流高峰)。我们会用。 阅读详情

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
   / \
  9  20
    /  \
   15   7

给出一个树的前序遍历和中序遍历,让重建这棵树

思路:
前序遍历数组的第一个元素一定是root

中序遍历的顺序是:左子树,root,右子树

根据前序遍历数组我们已经知道了root
根据这个root可以在中序遍历数组中找到root对应的位置rootIdx

几个变量:
前序遍历数组的第一个元素位置记为preStart
中序遍历数组的开始位置和结束位置记为:inStart, inEnd
同时可以知道因为inorder和preorder数组的size是一样的,所以inEnd也代表了preorder数组的结束位置

现在可以知道在中序遍历数组中从inStart到rootIdx-1是左子树,rootIdx+1到inEnd是右子树
同时也可以知道左子树的size是rootIdx - inStart + 1

前序遍历的顺序是:root, 左子树,右子树

现在前序遍历数组中从preStart+1到preStart+左子树size的范围是左子树
而左子树范围的第一个元素是左子树的root,然后再按上述步骤递归左子树范围的数组

同理preorder数组中preStart+左子树size+1到inEnd的范围为右子树的范围,
而右子树范围的第一个元素是右子树的root,然后递归右子树范围的数组

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || inorder == null) {
            return null;
        }
        
        return buildTreeHelper(preorder, inorder, 0, 0, inorder.length - 1);
    }
    
    public TreeNode buildTreeHelper(int[] preorder, int[] inorder, int preStart,
                                   int inStart, int inEnd) {
        
        if (preStart >= preorder.length || inStart > inEnd) {
            return null;
        }
        
        int rootIdx = inStart;
        TreeNode root = new TreeNode(preorder[preStart]);
        while (rootIdx < inEnd) {
            if (inorder[rootIdx] == preorder[preStart]) {
                break;
            }
            rootIdx ++;
        }
        
        root.left = buildTreeHelper(preorder, inorder, preStart + 1, inStart, rootIdx - 1);
        root.right = buildTreeHelper(preorder, inorder, preStart + rootIdx - inStart + 1,
                                     rootIdx + 1, inEnd);
        
        return root;
    }

或者

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

TreeNode build(int[] preorder, int[] inorder, int preS, int preE, int inS, int inE) {
    if(preS > preE || inS > inE) return null;
          
    int rootVal = preorder[preS];
    TreeNode root = new TreeNode(rootVal);
    
    int leftLen = 0;
    for(int i = inS; i < inE; i++) {
        if(inorder[i] == rootVal) break;
        leftLen ++;
    }
    
    root.left = build(preorder, inorder, preS+1, preS+leftLen, inS, inS+leftLen-1);
    root.right = build(preorder, inorder, preS+leftLen+1, preE, inS+leftLen+1, inE);
    
    return root;
}
pycharm报错cannot start the IED:Error occurred during initialization of VMagent library failed...如何解决? 🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。 阅读详情

相关推荐

在国产麒麟V10(arm64)上离线部署Docker 24.0.6和Compose 2.30.1,保姆级避坑实录

本文详细介绍了在国产麒麟V10(arm64)操作系统上离线部署Docker 24.0.6和Compose 2.30.1的完整流程,包括环境准备、架构认知、安装步骤、系统服务优化及生产环境加固。特别针对ARM64架构的兼容性问题提供了解决方案,帮助用户在信创环境中高效部署容器化技术。

weixin_30342209的博客 492

7-3 Left-View of Binary Tree (25 分)

7-3 Left-View of Binary Tree (25 分) Theleft-viewof a binary tree is a list of nodes obtained by looking at the tree from left hand side and from top down. For example, given a tree shown by the figure, its left-view is { 1, 2, 3, 4, 5 } Given the ino..

华师少女的梦的博客 335

别再手动敲代码了!用STM32CubeMX+FreeRTOS图形化配置,5分钟搞定多任务通信框架

本文详细介绍了如何利用STM32CubeMX和FreeRTOS快速搭建多任务通信框架,大幅提升嵌入式开发效率。通过图形化配置工具,开发者可在5分钟内完成任务调度、时钟配置和内核对象生成,特别适合物联网设备开发。文章还提供了代码优化和调试技巧,帮助开发者轻松应对复杂应用场景。

weixin_30363285的博客 299

PAT甲级2023夏季考试题解(A-1 Trap,A-2 Queue Using Two Stacks,A-3 Rank of Binary Tree,A-4 Big Number)

A-1 Trap分数 20作者 陈越单位 浙江大学。

qq_51825761的博客 2064

centos7 python3.12.1 报错 No module named _ssl,坑我几个小时

网上的教程很多,但几乎是无效的,跟着我这教程,可以完美解决。

Amio_的博客 1万+

MMSE (Minimum Mean Square Error)均衡原理讲解

MMSE是一种将接收数据的MSE(均方误差)最小化的模型。有了这句话,很多问题就会在你的脑海中蹦出来。 什么是均方误差?最小化MSE "的物理含义是什么? 等等。 让我们从一个我们现在已经非常熟悉的通道模型开始。(我希望你现在也熟悉下面的表达方式。) MMSE作为一种均衡器,是一种后处理算法,它可以帮助我们找出接收到的数据,使之尽可能地接近原始数据(传输数据)。简而言之,MMSE中最重要的步骤是找到一个如下图所示的矩阵G。如果我们假设没有噪声,这个[G]矩阵可以简单的作为信道矩阵(H^-1)的倒数。但

Celeste的博客 4万+

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

leo_weile的博客 273

LaTeX公式转Word竟这么简单?Python三行代码实现学术论文格式无忧

本文介绍如何利用Python的latex2word库,仅需三行代码即可将LaTeX数学公式精准转换为Word原生Office Math对象,解决学术写作中LaTeX与Word格式割裂的难题。该方法支持批量处理、格式保真,并能与Markdown、Jupyter Notebook等工具集成,构建自动化写作流程,实现学术论文格式无忧。

qsc90123456的博客 306

2020年PAT甲级秋季考试真题

1172 2020 秋季Panda and PP Milk(20) PP milk (盆盆奶)is Pandas’ favorite. They would line up to enjoy it as show in the picture. On the other hand, they could drink in peace only if they believe that the amount of PP milk is fairly distributed, that is, fatter

qq_40805977的博客 1351

MIPI DPHY接口(基于Xilinx FPGA实现)案例分析

在Xilinx FPGA实现MIPI DPHY接口的案例(包括CIS协议层)。截止目前为止,Xilinx仅在Ultrascale+及其以上版本的FPGA IO可直接支持MIPI 电平输入,其他的,都需要转换成LVDS来接收。

ONEFPGA的博客 5662

uniapp [全端兼容] - 详细实现全局修改设置字体大小功能效果,统一修改页面字号字体大小,一键改变字号整个项目立即生效,大中小字号预设、老年人大字模式场景,uniApp全平台兼容控制页面字号大小

uni-app,h5移动端网页,微信小程序,支付宝小程序,Android安卓,苹果ios,app,nvue,修改字体大小,一键调整UniApp字体大小,实时改变不用刷新,动态设置字体大小,uniapp全局修改字体字号大小,如何控制全局字号,可调整显示的字体,全项目生效,老年人大字模式,改变所有页面的字体,全局设置字体大小,实现小中大的字体切换,设置改变全局字体大小功能,如何做到全平台兼容,H5APP实现全局字体大小调整,不兼容报错,用户设置字号,全局设置应用字体大小,大中小字体修改,页面设置page-me

🏆 前端领域优质创作者 1万+

105. Construct Binary Tree from Preorder and Inorder Traversal

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

猫敷雪 412

【ArcPy】用Python批量处理大规模地理数据的几何校正

在许多GIS应用中,尤其是在大规模地理数据处理时,几何校正(Geometric Correction)是一个至关重要的任务。它确保了不同来源的地理数据能够正确地对齐和匹配,从而为后续分析提供准确的基础。几何校正的常见应用包括遥感影像的地理配准、矢量数据的投影转换等。几何校正通常包括坐标转换、重采样等步骤。以下示例展示了如何使用arcpy的工具进行几何校正。"""实施几何校正:param input_layer: 输入待校正数据层。

陈书予 1019

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

salmonwilliam的博客 394

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

前序和中序遍历重建二叉树

grllery的博客 494

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal

problem: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags  Tree Array Depth-first

hustyangju的足迹 858

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的博客 393

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

卷卷萌的博客 218

【LeetCode 面试经典150题】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序和中序构造二叉树

Given two integer arrays and where is the preorder traversal of a binary tree and is the inorder traversal of the same tree, construct and return the binary tree.给定两个整数数组 和 ,其中 是二叉树的前序遍历,而 是同一棵树的中序遍历,根据这两个数组构造并返回这棵二叉树。输入: = [3,9,20,15,7], = [9,3,1

qq_43513394的博客 548

【LeetCode 105】Construct Binary Tree from Preorder and Inorder Traversal

题意: 给出二叉树的前序和中序遍历,建造出二叉树。 思路: dfs,根据前序的第一个值是树的根节点,在中序中找到根节点,然后分为左右子树,这样递归解决。 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *...

iCode_girl的博客 135

从前序与中序遍历序列构造二叉树(LeetCode 105):递归分治与哈希表索引推导详解

本文以《Krahets 笔面试精选 88 题》中 105. 从前序与中序遍历序列构造二叉树 一题为载体,系统讲解如何利用**前序遍历确定根节点、中序遍历划分左右子树**的递归分治思想还原整棵二叉树。你将掌握:前序/中序遍历的区间划分性质、递归子问题的索引推导公式、用哈希表将查找根节点的时间降为 O(1) 的优化技巧,并对照仓库内 Python / Java / C++ 三套源码理解实现细节,最终能

gitblog_01141的博客 458

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 .

taoqick的专栏 205

MAS Windows/Office 激活脚本完整指南:四种方法选对一步到位

屏幕角落那块黑乎乎的"激活 Windows"水印,加上一打开 Office 就飘灰的提醒,是不是你已经忍了很久?多数人第一反应是到处找激活工具,但说实话,你真正需要的可能只是一套开源脚本:Microsoft Activation Scripts(MAS)。它提供 HWID、Ohook、TSforge、Online KMS 四种激活方法,Windows 和 Office 都能管。读完这篇文章,你会清

gitblog_00896的博客 205

Leetcode Golang 105. Construct Binary Tree from Preorder and Inorder Traversal.go

思路 根据前序遍历和中序遍历的结果数组,重新构建二叉树 前序的第一个元素就是数的root节点,从中序中找到根节点,则左边的都是左子树,右边的都是右子树 然后递归 code type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func buildTree(preorder []int, inorder []in...

anakinsun的博客 212

C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告...

剑指offer重建二叉树提交网址:http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157或 leetcode 105:https://leetcode.com/problems/construct-binary-tree-from-pre...

weixin_33801856的博客 655
上一篇: Java Singleton Design Pattern (单例模式 SE7 Gold)
下一篇: leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal(中序和后序遍历数组中恢复二叉树)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址