Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode

Imputation Methods for scRNA Sequencing Data 越来越多的研究者使用单细胞RNA测序(scRNA-seq)技术来表征单细胞水平的转录图谱。他们用它来研究复杂组织的异质性、转录组动力学和未知生物的多样性。然而,在基因表达模式随机性的scRNA-seq数据中普遍存在许多技术和生物学上的噪音。这些数据通常具有高维、稀疏和大量“dropout”值的特点,并且受批处理效应的影响。scRNA-seq数据中大量的“dropout”值严重掩盖了基因之间的重要关系,阻碍了下游分析。因此,scRNA-seq数据dropout值的估算就显得尤为重要。 阅读详情

经典的题目,而且在实际中也有着用途,比如如何把一个二叉树序列化后通过网络传给另一台机器,我们就可以把二叉树以preorder和inorder的方式变成string传给另一台机器,另一台机器再重建二叉树。


package Level4;

import Utility.TreeNode;

/**
 * 
 * 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.
 * 
 */
public class S140 {

	public static void main(String[] args) {
		int[] preorder = {1};
		int[] inorder = {1};
		buildTree(preorder, inorder);
	}

	public static TreeNode buildTree(int[] preorder, int[] inorder) {
		int len = preorder.length;
		if(len == 0){
			return null;
		}
		return rec(preorder, inorder, 0, len, 0, len);
	}
	
	public static TreeNode rec(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd){
		if(preStart >= preorder.length){		// 超过范围!
			return null;
		}
		TreeNode root = new TreeNode(preorder[preStart]);
		int rootIndex;
		for(rootIndex=inStart; rootIndex<=inEnd; rootIndex++) {
			if(inorder[rootIndex] == preorder[preStart]){	// 在inorder中找到root的位置
				break;
			}
		}
		
		int len = rootIndex - inStart;		// 计算距离差
		if(rootIndex > inStart){
			root.left = rec(preorder, inorder, preStart+1, preStart+len, inStart, rootIndex-1);
		}
		if(rootIndex < inEnd){
			root.right = rec(preorder, inorder, preStart+len+1, preEnd, rootIndex+1, inEnd);
		}
		
		return root;
	}
	
}


下面是另一种写法,避免了preStart的变化,但缺点是要用一个全局变量记录preIndex

int preIndex = 0;
	
	public TreeNode buildTree(int[] preorder, int[] inorder) {
		return rec(preorder, inorder, 0, inorder.length-1);
	}
	
	public TreeNode rec(int[] preorder, int[] inorder, int inStart, int inEnd) {
		
	    if (inStart > inEnd) {
	        return null;
	    }
	    
	    int rootData = preorder[preIndex++];
	    TreeNode root = new TreeNode(rootData);
	    
	    int inIndex = search(inorder, inStart, inEnd, rootData);
	    
	    root.left = rec(preorder, inorder, inStart, inIndex-1);
	    root.right = rec(preorder, inorder, inIndex+1, inEnd);
	    
	    return root;
	}
	
	private int search(int[] inorder, int inStart, int inEnd, int data) {
	    for(int i=inStart; i<=inEnd; i++) {
	        if(inorder[i] == data) {
	            return i;
	        }
	    }
	    return -1;
	}



/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return rec(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
    }
    
    public TreeNode rec(int[] preorder, int[] inorder, int preorderLeft, int preorderRight, int inorderLeft, int inorderRight) {
        if(preorderLeft > preorderRight) {
            return null;
        }
        
        TreeNode root = new TreeNode(preorder[preorderLeft]);
        int inorderRootPos = 0;
        for(; inorderRootPos < inorderRight; inorderRootPos++) {
            if(inorder[inorderRootPos] == root.val) {
                break;
            }
        }
        
        int dist = inorderRootPos - inorderLeft;
        root.left = rec(preorder, inorder, preorderLeft+1, preorderLeft+dist, inorderLeft, inorderRootPos-1);
        root.right = rec(preorder, inorder, preorderLeft+dist+1, preorderRight, inorderRootPos+1, inorderRight);
        return root;
    }
}




OrangePi Kunpeng Pro 安装 ROS2 + Gazebo OrangePi Kunpeng Pro 的优点很明显,内置openEuler,配备PD电源,做到开箱即用;板子设计也不错,风扇噪音不大,散热良好,昇腾芯片又确保小板子的性能,接口丰富是这类板子共同的优点,也很好的继承了下来,甚至还可以外接固态,可靠性大大提升。使用起来,哪怕不用openEuler的话,也可以很方便的刷Ubuntu来实现一些需要依赖的开发工作,这次没有跑AI模型,所以对算力的优势还没有很明显的感受,下次可以试一下yolo跑跑看。 阅读详情

相关推荐

用4690张图片搞定物流仓储难题:手把手教你训练YOLOv8纸箱检测模型(附完整源码)

本文详细介绍了如何利用YOLOv8目标检测技术解决物流仓储中的纸箱识别难题。通过4690张图片构建高质量数据集,提供完整的训练教程和源码,实现98.7%的mAP@0.5准确率。文章涵盖数据采集、模型训练、调参技巧到边缘计算部署的全流程,帮助读者快速构建智能仓储检测系统。

weixin_28717939的博客 378

【数据结构】二叉树 (Binary Tree)

二叉树快速入门

Living_Amethyst的博客 2683

Codex驱动AEDT完成无线充电仿真——从双线圈建模到三维电磁场动画

本文介绍了一个利用Codex、PyAEDT和Ansys Electronics Desktop 2025 R2实现双线圈无线充电系统自动建模与仿真的案例。通过简化模型(平面线圈+铁氧体磁芯),完成了从参数化建模、电磁场求解到后处理的全流程自动化,获得了耦合系数(k=0.17)、三维电磁场分布及多周期动态可视化效果。研究发现:线圈间距对传输效率影响最大(10-30mm间距时效率从40.2%降至5.57%),横向偏移15mm会使效率下降35%,而磁芯厚度增加带来的效率提升有限(3→7mm仅提高1.67%)。该框

sjjsjjsjjsjjsjj的博客 447

创建二叉树(利用前序遍历结果)

/** * 前序遍历结果创建二叉树 * @param data */ public void creatBinaryTreePre( ArrayList<String> data ){ creatBinaryTree(data.size(),data); } private TreeNode creatBinaryTree(int size, ArrayLi...

qq_40068214的博客 431

离线环境下 Ubuntu 22.04 制作自定义apt源 安装 Docker环境 的部署方案

本文介绍了一种在无外网环境下部署服务器项目的离线方案。通过在外网环境中制作包含APT软件包和Docker镜像的离线资源包,实现在内网环境中的完整部署。方案详细说明了离线APT源的制作方法、Docker镜像的导出/导入流程,以及内网服务器的软件安装和网络配置步骤。特别强调了版本一致性、目录结构规范和网络适配等注意事项,适用于需要严格隔离网络的生产环境部署。该方案具有可扩展性,可根据实际需求调整软件包和镜像列表。

mmx_2022的博客 654

声品质粗糙度matlab,声品质的应用分析方法

声品质的应用分析方法2016-04-05 09:18:57阅读次数:14198摘要:声品质分析在噪声的主观评价方面已经得到越来越多的应用。本文比较了声品质分析与传统计权声级的区别,并介绍了在声品质分析的实际应用中,针对稳态信号、非稳态信号的几种不同分析方法,以及各分析方法的要求和特点。关键词:声品质,响度,尖锐度,粗糙度,波动度1前言传统评价噪声的方法主要为计权声压级,用以反映声音的大小。但是人耳...

weixin_30767323的博客 2439

c++ 结构体遍历_二叉树(Binary Tree)的建立与遍历——C语言实现

一、运行环境简介编辑器:VSCode + MicroSoft原生插件;:cat:‍:dragon:运行环境: MinGW ;:cat:‍:bust_in_silhouette:常用指令: gcc mian.c -o mian.exe二、二叉树的定义这里我们直接采用浙大数据结构课程中的代码。因为这种写法清晰明了,且便于后续扩展。typedef char ElementType; typedef s...

weixin_39862097的博客 509

如何选择汽车域控制器MCU?一文看透7大关键维度[特殊字符](含芯片推荐、踩坑预警)

MCU选型 Checklist(实用表格)维度你需要考虑的问题Flash/RAM我的任务栈够吗?是否支持OTA双Bank?安全等级这功能出故障会不会危及人命?接口资源够几个CAN?支持CAN FD?ETH是否支持TSN?主频我的软件响应周期是多少?是否带Cache?多核架构是SMP/AMP?是否带锁步核?跑多个任务会不会互相干扰?功耗与封装睡眠电流够低?有外扩DDR空间?BGA能焊?供应链有无国产替代?交期是否稳定?支持AUTOSAR吗?

weixin_43199439的博客 663

算法系列——二叉树先序遍历(Binary Tree Preorder Traversal)

题目描述Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3].Note: Recursive solution is triv

BridgeGeorge 1643

ABAP Development Tool for Visual Studio Code 里使用 MCP server

本文讨论了SAP ABAP开发环境中,除GitHub Copilot外,其他可作为MCP客户端接入ABAP开发工具(MCP server)的编码代理(coding agent)。GitHub Copilot是官方确认可用的工具,而其他代理如Claude Code、Cursor、Windsurf Cascade、Cline和Continue虽然理论上支持MCP协议,但要实际用于ABAP开发还需满足两个关键条件:兼容MCP协议,并能处理VS Code虚拟工作区文件系统。这些工具需要单独验证是否支持ABAP开发工

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 259

开源的预测气象大模型与数据获取/可视化

「开源气象大模型与数据获取指南」摘要: 当前主流AI气象模型(如ECMWF的AIFS、DeepMind的GraphCast、华为Pangu等)主要基于ERA5/HRES数据训练。开源资源可通过ECMWF OpenData、GitHub仓库及Google Cloud获取,其中AIFS数据采用CCBY-4.0许可可商用,其他模型多有限制性许可。典型应用流程包括:1)通过Python包获取实时预报数据;2)加载预训练权重进行推理;3)使用xarray/cartopy或Zarr进行可视化。训练数据可从CDS下载ER

专注AI大模型,软件混淆,授权 795

Leetcode: 先序遍历构造二叉搜索树(Construct Binary Search Tree from Preorder Traversal)(C++)

Return the root node of a binarysearchtree that matches the givenpreordertraversal. (Recall that a binary search treeis a binary tree where for everynode, any descendant ofnode.lefthas a valu...

qq_30811835的博客 456

Transformer实战(2)——循环神经网络详解

循环神经网络 (Recurrent neural networks, RNN) ,RNN 是在文本输入中广泛使用的神经网络。RNN 非常灵活,已被用于解决诸如语音识别、语言建模、机器翻译、情感分析和图像字幕等问题。RNN 利用输入的序列特性,序列输入可以是文本、语音、时间序列以及任何其他元素的出现取决于其前面元素的序列。在本节中,我们介绍了 RNN 及其变体,并学习如何使用 TensorFlow 进行实现。

盼小辉丶的博客 1万+

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

负雪明烛 3809

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【殷人昆数据结构】第四章5.1 完全二叉树BinaryTree代码的调试

二叉树BinaryTree 主函数 #include "BinaryTree.h" #include <iostream> #include <fstream> #include <iomanip> using namespace std; void visit(BinTreeNode<int> *t){ cout<<t->data<<" "; } int main(){ ifstream fin("data.txt"

tianty1121的博客 1010

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

ssm框架搭建

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

binary-tree-preorder-traversal(二叉树前序遍历)

题目描述 Given a binary tree, return the preorder traversal of its nodes’ values. For example:Given binary tree{1,#,2,3},return[1,2,3]. Recursive solution is trivial, could you do it iteratively? 翻译:给定二...

傻月菇凉的博客 2165

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

#include<iostream> #include<vector> #include<map> using namespace std;struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),right(NULL){}; };

chencong125的博客 256

leetcode第44题(construct-binary-tree-from-preorder-and-inorder-……)

题目: Given preorder and inorder traversal of a tree, construct the binary tree.  Note:  You may assume that duplicates do not exist in the tree. 思路: 前序遍历是根左右,中序遍历是左根右,第一个节点是跟,中序中根的左边为左子树,根

u012338652的博客 280

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历序列构造二叉树)

HashMap<Integer, Integer> map = new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder) { int len = inorder.length; if(len == 0) return null; ...

肉丸不肉 412

二叉树(BinaryTree)的建立和遍历

//BinaryTree.h struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }; void createBinaryTree1(BinaryTreeNode** roo...

akenseren的博客 5730

[LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal

根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点.样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3Given preorder and inorder traversal of a tree, construct the binary tree. Notice You may assume

wutingyehe的专栏 625

Construct Binary Tree from Preorder and Inorder Traversal

根据前序和中序序列构建一颗二叉树。

不积跬步无以至千里 237

C++ 二叉树(BinaryTree)的基本操作

一、实现程序 // 题目:实现二叉树的各种操作 // 测试数据:1 2 # 3 # # 4 5 # # 6 # # #include &lt;iostream&gt; #include &lt;queue&gt; #include &lt;stack&gt; template&lt;class T&gt; struct Node { T data; Node *next;...

ChanJose的博客 4633

Construct Binary Tree from Preorder and Postorder Traversal(C++根据前序和后序遍历构造二叉树)

Construct Binary Tree from Preorder and Postorder Traversal(C++根据前序和后序遍历构造二叉树)

coolsunxu的博客 143

数据结构 二叉树(binarytree)的实现

我们先来介绍一些关于树的概念:节点:结点包含数据和指向其它节点的指针。 根节点:树第一个结点称为根节点。 结点的度:结点拥有的子节点个数。 叶节点:没有子节点的节点(度为0)。 父子节点:一个节点father指向另一个节点child,则child为孩子节点,father为父亲节点 。 兄弟节点:具有相同父节点的节点互为兄弟节点。 节点的祖先:从根节点开始到该节点所经的所有节点都可以称为该

ChaseRaod的博客 972
上一篇: Decode Ways 把字符串解码成数字组合@LeetCode
下一篇: 记毕业季的求职经历 remlostime
chiiis
博客等级 码龄14年 422粉丝 429原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值