LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal

Apollo学习笔记(1) 百度 Apollo Cyber RT简介、基本概念以及与 ROS 对照 一、背景 ROS 应用于自动驾驶领域的不足: 调度的不确定性:各节点以独立进程运行,节点运行顺序无法确定,因而业务逻辑的调度顺序无法保证; 运行效率:ROS 为分布式系统,存在通信开销 二、Cyber RT 框架 从下到上依次为: 基础库:高性能,无锁队列; 通信层:Publish/Subscribe机制,Service/Client机制,服务自发现,自适应的通信机制(共享内存、Socket、进程内); 数据层:数据缓存与融合。多路传感器之间数据需要融合,而且算法可能需要缓存一定的数据。比如典型的仿真应用 阅读详情

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

Note:

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

像一般的tree的问题一样,这题也用递归比较好。

比如这样子的序列:

preorder: 1245367

inorder: 4251637

可以一眼看出的是1一定是这颗树的root,因为pre order traversal是先访问节点本身再访问子树的。再看看in order traversal里面1的位置,是在中间。按照Inorder的定义,先访问左子树再访问右子树。因此可以知道在In order traversal的序列中排在1前面的都是1的左子树的元素,排在1后面的都是1的右子树的元素。对于序列425,其pre order traversal必然是245;对于序列637,其pre order traversal必然是367.这样就有了一个基本的递归。

public class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        TreeNode root = buildTree(preorder, inorder, 0, 0, inorder.length - 1);
        return root;
    }
    
    public TreeNode buildTree(int[] preorder, int[] inorder, int preIndex, int inStart, int inEnd){
        if(inStart > inEnd)
            return null;
        TreeNode node = new TreeNode(preorder[preIndex]);
        if(inStart == inEnd)
            return node;
        else{
            int inIndex = search(inorder, preorder[preIndex]);
            int len = inIndex - inStart;
            node.left = buildTree(preorder, inorder, preIndex + 1, inStart, inIndex - 1);
            node.right = buildTree(preorder, inorder, preIndex + len + 1, inIndex + 1, inEnd);
            return node;
        }
    }
    public int search(int[] arr, int value){
        int index = -1;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == value){
                index = i;
                break;
            }
        }
        return index;
    }
}



Open3D(C++) 点云构建三维Delaunay三角剖分 C++版本的Open3D实现从点云构建三维Delaunay,可视化效果。 阅读详情

相关推荐

别再让OCV拖慢你的芯片!手把手教你用set_timing_derate优化STA时序(附CPPR实战)

本文深入探讨了芯片设计中OCV(On-Chip Variation)对时序分析的影响,并提供了使用`set_timing_derate`优化STA时序的实战指南。通过分阶段derate设置和CPPR技术,有效减少时序违例,提升芯片性能。文章结合TSMC 7nm工艺案例,详细解析了OCV建模与优化策略,助力工程师实现时序收敛。

weixin_42525005的博客 304

Largest BST in a Binary Tree

一到面试题,挺有意思的。 简单做法是对于每个node都进行一遍isBST的测试,找出最大的那个。 public static int largestBSTNaive(TreeNode root){ if(isBST(root)){ return size(root); } return Math.max(largestBSTNaive(root.left), largestB

likecool21的专栏 1969

SAP MM新手避坑指南:工厂间调拨用301还是303?手把手教你选对移动类型

本文深入解析SAP MM模块中工厂间调拨的301与303移动类型,帮助新手避开常见陷阱。通过对比一步法(301)和两步法(303+305)的核心差异、适用场景及财务影响,提供清晰的决策框架和实战技巧,确保库存调拨的准确性和效率。

weixin_29223603的博客 346

LeetCode刷题笔录Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit st

likecool21的专栏 1134

GD32F407工程模板搭建全记录:从零整理固件库、创建文件夹到编译通过

本文详细记录了GD32F407工程模板的搭建过程,从固件库整理、目录规划到Keil环境配置,帮助开发者快速搭建可维护的嵌入式工程结构。重点介绍了开发环境搭建、工程目录设计、Keil配置及常见编译问题解决方案,适合嵌入式开发者参考。

weixin_29164497的博客 118

RouterOS v7.6实战:5分钟搞定IPv6双栈配置(含光猫桥接避坑指南)

本文详细解析了在RouterOS v7.6系统中配置IPv6双栈网络的实战步骤,特别针对家庭网络光猫桥接模式下的常见问题提供了避坑指南。文章涵盖了从基础配置到企业级部署的进阶内容,包括IPv6前缀获取、DHCPv6客户端设置、路由通告以及安全优化,帮助用户充分利用v7.6版本在IPv6支持上的改进与优势。

embedding5hiker的博客 1275

LeetCode刷题笔录Subsets

自己总是写不出递归的方法,只能用f

likecool21的专栏 1121

西红柿成熟度检测数据集总结

成熟度类别标注数量semiripe(半成熟)10275rotten(腐烂)13470fullyripe(完全成熟)35031unripe(未成熟)26212。

crz1336768325的博客 2125

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

用AI+Playwright实现零代码UI自动化测试:以登录页面为例的实战指南

本文详细介绍了如何利用AI与Playwright结合实现零代码UI自动化测试,特别以登录页面为例进行实战演示。通过自然语言描述生成测试脚本,大幅降低技术门槛,提升测试效率,并支持跨浏览器验证。适合非技术人员快速上手专业级自动化测试。

weixin_28842269的博客 401

部署OpenClaw(ClawdBot、MoltBot)后访问localhost:18789报1008错误怎么办?

部署openclaw(曾用名ClawdBot、MoltBot)后访问localhost:18789出现1008授权错误❌?本文分享如何快速解决这个问题✅

XiaoqiangClub的博客 1万+

LeetCode刷题笔录Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3

likecool21的专栏 1016

2025.05.28【读书笔记】|如何用SILVA和RFAM数据库高效去除rRNA污染

摘要: 在Ribo-seq等测序数据分析中,清除rRNA污染对结果准确性至关重要。仅使用SILVA数据库可能遗漏5S rRNA污染,建议结合RFAM数据库(特别是RF00001家族)全面清除。操作要点包括:下载两库序列、将RNA碱基U替换为DNA的T、合并序列建立索引,使用bowtie2过滤rRNA reads。注意U/T转换是关键,否则会导致比对失败。该方法能有效提升数据清洁度,为后续分析奠定基础。

980

LeetCode刷题笔录Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "

likecool21的专栏 923

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

visio安装包

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

LeetCode刷题笔录Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \

likecool21的专栏 864

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

LeetCode刷题笔录 Same Tree

public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; else if(p == null || q == null) return f

likecool21的专栏 775

LeetCode刷题笔录Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]

likecool21的专栏 760

LeetCode刷题笔录 Pow(x,n)

Implement pow(x, n). z

likecool21的专栏 752

LeetCode刷题笔录Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devis

likecool21的专栏 750

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 111

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 352

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 202

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 175
上一篇: LeetCode刷题笔录Binary Tree Post Order Traversal
下一篇: LeetCode刷题笔录Recover Binary Search Tree
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值