105 - Construct Binary Tree from Preorder and Inorder Traversal

MFC停靠窗口实战:CDockablePane从创建到控件布局的完整指南(附残影问题排查) 本文提供了MFC中CDockablePane停靠窗口的完整实战指南,涵盖从自定义窗格类创建、主框架集成、内部控件布局到高级功能定制的全过程。针对开发中常见的“残影”视觉问题,文章深入剖析了其根源,并给出了系统性的排查步骤与解决方案,帮助开发者构建稳定、专业的可停靠用户界面。 阅读详情

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

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

Subscribe to see which companies asked this question

思路分析:

题意:也就是根据树的前序中序遍历来重构二叉树。

There is an example.
        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder:  {7}, {10,4,3,1}, {2,8,11}
inorder:     {4,10,3,1}, {7}, {11, 8,2}

        _______7______
       /              \
    {4,10,3,1}       {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.
2nd round
left part                                                                            right part
preorder: {10}, {4}, {3,1}                                              {2}, {8,11}
inorder:  {4}, {10}, {3,1}                                                {11,8}, {2}


        _______7______
       /              \
    __10__          ___2
   /      \        /
   4      {3,1}   {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.

Same way to split {3,1} and {11,8}, yo will get the complete tree now.

        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
So, simulate this process from bottom to top with recursion as following code.
代码如下:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution_105_ConstructBinaryTreefromPreorderandInorderTraversal
{
public:
	TreeNode *BuildTreePI(vector<int> &preorder, vector<int> &inorder, int p_s, int p_e, int i_s, int i_e)
	{
		


		int pivot = preorder[p_s];
		int i = i_s;

		for (; i < i_e; i++)
		{
			if (inorder[i] == pivot)
			{
				break;
			}
		}

		int length1 = i - i_s - 1; //中序遍历中的root在向量中的下标
		int length2 = i_e - i - 1; //中序遍历中从后向前的元素个数
		TreeNode *node = new TreeNode(pivot);

		node->left = BuildTreePI(preorder, inorder, p_s + 1, length1 + p_s + 1, i_s, i - 1);
		node->right = BuildTreePI(preorder, inorder, p_e - length2, p_e, i + 1, i_e);
	}
	TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) 
	{
		return BuildTreePI(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
	}
};



LIS3DHTR三轴加速度计Arduino驱动与嵌入式集成指南 加速度计是嵌入式系统中实现运动感知、姿态解算和振动分析的核心传感器。其工作原理基于MEMS微机械结构在惯性力作用下的电容变化,经片内ADC转换为数字信号;I²C接口因其硬件资源占用少、布线简洁,成为低功耗物联网节点的主流通信方式。技术价值体现在高分辨率(12位)、多量程(±2g~±16g)与低功耗(待机电流仅2µA)的平衡,支撑电池供电设备长期运行。典型应用场景包括可穿戴设备的姿态识别、工业预测性维护中的振动监测,以及智能硬件的敲击/跌落检测。本文围绕LIS3DHTR芯片及其Grove模块,详解Arduin 阅读详情

相关推荐

分享一下使用Xilinx FPGA驱动MIPI DSI LCD屏的心路历程

分享一下调试用Xilinx FPGA驱动MIPI DSI LCD屏的心路历程,看到的朋友们,可以避免继续踩坑。

haoxingheng的专栏 5017

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. 示例:其实这个图我在上一篇文章就给出了,

漫漫学习路 274

避坑指南:在Ubuntu 20.04上从零部署IS-Fusion环境(含CUDA 11.1和PyTorch 1.10.1配置)

本文提供了在Ubuntu 20.04系统上从零部署IS-Fusion 3D目标检测环境的详细避坑指南。内容涵盖CUDA 11.1、PyTorch 1.10.1、spconv等核心组件的精确版本安装与兼容性配置,并指导如何准备nuScenes数据集及启动模型训练,旨在帮助研究者和开发者高效搭建稳定的多模态融合算法开发环境。

gitlab7runner的博客 553

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal, Solution

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. » Solve this problem [Thoughts] There is an example. __

codingtmd_in_china的博客 251

【黑马点评优化】3-压测秒杀接口,算出性能塌陷区,之后用sentinel实现秒杀接口限流

【黑马点评优化】3-压测秒杀接口,算出性能塌陷区,之后用sentinel实现秒杀接口限流

m0_52031708的博客 3571

NFS服务器配置与autofs自动挂载

NFS服务器配置autofs自动挂载 环境: hostname ip OS server 192.168.220.138 centos7 client 192.168.220.139 centos7 一、NFS概述 NFS(Network File System)网络文件系统。最大功能就是通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。 二、端口 [root@se...

DoloresOOO的博客 1028

按照前序遍历和中序遍历构建二叉树

转载自:http://blog.youkuaiyun.com/sbitswc/article/details/26433051 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tre

jyl1999xxxx的博客 374

手把手教你用Cadence仿真12bit SAR ADC:从电路图到FFT频谱分析(含Simc 18mmrf工艺文件)

本文详细介绍了使用Cadence仿真12bit SAR ADC的全流程,包括电路搭建、仿真环境配置、瞬态仿真优化及FFT频谱分析。基于Simc 18mmrf工艺文件,提供从电路图到性能验证的实战指南,特别分享工程实践中的排错技巧和优化策略,帮助工程师高效完成ADC设计验证。

weixin_30832405的博客 406

Binary Tree Traversals(二叉树的遍历)

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which th...

程帅霞 818

ADS软件操作(10)——DAC控件使用

该控件可以用于为元件定义随频率变换的参数,如放大器的最佳负载阻抗或者源阻抗,需要双频匹配时的优化等,下图是ADS中该控件的模型。

Zhuanzhuyan的博客 4413

RTKlib操作手册--使用样例数据演示

本文将使用样例数据演示如何使用RTKLIB进行rtk差分定位。

samur2的博客 7786

Binary Tree Traversals(ACM训练 - 对顶堆学习笔记)

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which th...

Spike的秃头之路 277

【用unity实现100个游戏之2】复刻实现经典2d平台跳跃游戏《蔚蓝 Celeste》(附工程源码)

蔚蓝》是一款备受好评的独立游戏,由游戏开发工作室开发并由发行。这款游戏于2016年首次发布,并在玩家和评论界收获了广泛赞誉。蔚蓝以其独特的艺术风格和引人入胜的故事而著称。游戏以深海为背景,玩家将扮演一个能够探索水下世界的神秘生物。游戏通过惊人的视觉表现和动态的海洋生态系统为玩家带来了令人难忘的游戏体验。无论是广阔的水下景观还是充满生机的海洋生物,蔚蓝都让玩家感受到深海的神秘和美丽。蔚蓝在玩法上注重探索和解谜。玩家可以在海底环境中自由游动,发现隐藏的地点和秘密,与各种生物互动,并解决环境谜题以继续前进。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 4170

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

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

crazy_jack 8975

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

visio安装包

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

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

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

OCEANtroye的博客 442

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

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

哈哈的个人专栏 1688

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

105. Construct Binary Tree from Preorder and Inorder Traversal

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

猫敷雪 411

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

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

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

shadowkun的博客 197

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

原题链接:

Ber03的博客 238

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

本题是二叉树的经典问题,主要考察对二叉树遍历和递归的理解和应用。通过使用递归,我们可以根据先序遍历和中序遍历的结果构造二叉树。递归的核心思想是:先序遍历的第一个元素是根节点,在中序遍历中找到根节点的位置,然后递归地构造左子树和右子树。这种方法不仅适用于从前序与中序遍历序列构造二叉树问题,还可以应用于许多其他需要根据遍历结果构造二叉树的场景。掌握递归的思想,对于解决这类问题非常重要。

cannonjinx的博客 4183

FTPrep, 105 Construct Binary Tree from Preorder and Inorder Traversal

这题是套路,看过一遍就应该很容易理解记忆了。后面的题是变体,通过inorder + postorder 来构建一棵树。所以inorder是必须的,然后preorder 和 postorder 有一个就okay了。 关键一点是找到inorder中root的位置,可以根据root.value来找。然后为了查询 inorder中的root index 简单容易,通过构建一个HashMap就可以。 代

laserljy123的博客 299

LeetCode105—Construct Binary Tree from Preorder and Inorder Traversal

LeetCode105—Construct Binary Tree from Preorder and Inorder Traversal

上善若水 830

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

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

chenf1999的博客 345
上一篇: 69 - Sqrt(x)
下一篇: 206 - Reverse Linked List
hellochenlu
博客等级 码龄14年 97粉丝 184原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值