LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]

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

【题目】

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

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



【题意】

    给定中序遍历和后序遍历,构造二叉树


【思路】


    思路跟Construct Binary Tree from Preorder and Inorder Traversal相同, 只不过是前序换成了后序,前序的构成是:根值+左子树先序+右子树先序;而后序的构成是:左子树后序+右子树后序+根值。


【代码】

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int getPosOfRoot(vector<int> &inorder, int istart, int iend, int key){
        for(int i=istart; i<=iend; i++){
            if(inorder[i]==key)return i;
        }
    }

    TreeNode *build(vector<int> &inorder, int istart, int iend, vector<int>&postorder, int pstart, int pend){
        if(istart>iend)return NULL;
        //创建根节点
        TreeNode*root = (TreeNode*)malloc(sizeof(TreeNode));
        root->val=postorder[pend];
        
        //确定根值在中序遍历中的位置
        int rootPos = getPosOfRoot(inorder, istart, iend, postorder[pend]);
        //左子树的序列长度
        int lenLeft = rootPos-istart;
        //右子树的序列长度
        int lenRight = iend-rootPos;
        //构造左子树
        TreeNode*left = build(inorder, istart, rootPos-1, postorder, pstart, pstart+lenLeft-1);
        //构造右子树
        TreeNode*right = build(inorder, rootPos+1, iend, postorder, pstart+lenLeft, pend-1);
        
        //构造二叉树
        root->left=left;
        root->right=right;
        
        return root;
    }

    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        int size=inorder.size();
        if(size==0)return NULL;
        
        return build(inorder, 0, size-1, postorder, 0, size-1);
    }
};


c++多线程(1)------创建和管理线程td::thread std::thread 是 C++11 标准库中用于创建和管理线程的核心类,定义在 <thread> 头文件中。它使得多线程编程变得简单、类型安全且跨平台。 阅读详情

相关推荐

《Linux运维总结:etcd 3.5.15集群数据备份与恢复》

《Linux运维总结:etcd 3.5.15集群数据备份与恢复》

东城绝神 1083

【LeetCode 面试经典150题】106. Construct Binary Tree from Inorder and Postorder Traversal 中后序遍历构造二叉树

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

qq_43513394的博客 506

VC Spyglass CDC实战:如何快速定位和解决重汇聚问题(附详细调试步骤)

本文深入探讨了在SoC设计中利用VC Spyglass进行CDC验证时,如何高效定位和解决棘手的重汇聚问题。通过实战演练,详细解析了重汇聚的本质、VC Spyglass的检测逻辑,并提供了从报告筛选、GUI原理图分析到制定设计修复或约束豁免方案的全套调试步骤,帮助工程师快速攻克这一常见验证难题。

weixin_29219539的博客 765

LeetCode OJ 之 Construct Binary Tree from Inorder and Postorder Traversal (由二叉树的中序和后序序列构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给定一棵树的前序和后序遍历结果,构造这棵二叉树,假设二叉树不存在重复的元素。 思路:

灯火阑珊处 603

全球气候模式降尺度方法总结及其代码实现

简介总结各类型气候模式降尺度方法

WW、forever的博客 1万+

NTC热敏电阻选型快速指南

NTC(Negative Temperature Coefficient Thermistor)负温度系数热敏电阻器,是由锰Mn,镍Ni,铜Cu,一氧化碳Co等烧结而成的氧化物。其电阻值随温度的升高而减小,即具有复温度系数,故被称为负温度系数热敏电阻。本文对热敏电阻的应用场景,结构特点和选型参数进行总结。

fydar的博客 1308

Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, giveninorder = [9,3,15,20,7] postorder = [9,15,7,20...

zrh_CSDN的博客 343

【施磊】C++课程笔记目录

【C++编程】施磊老师C++笔记目录

GeniusAng的博客 4157

LeetCode 106-Construct Binary Tree from Inorder and Postorder Traversal(二叉树,后序+中序序列构造二叉树)

LeetCode 106-Construct Binary Tree from Inorder and Postorder Traversal题解

祝如愿的博客 216

FAST-LIVO2 源码精读(十六):接入自定义传感器——多雷达适配与内外参标定

本文是「FAST-LIVO2 激光-惯性-视觉里程计源码精读」专栏第十六篇。上一篇拆解了中每个关键参数的物理含义与调参路径,并反复强调外参RclPcl与时间偏移配错即全局失效;本篇承接这一悬念,回答更前置的工程问题——如何把 FAST-LIVO2 接到一套全新硬件上:新雷达如何通过的类型分支适配、相机内参与畸变模型如何配置、两组外参与时间偏移如何标定出来并填入 YAML,最后给出一份落地检查清单。

PX4-SLAM-PNC 206

【一 扣子客户案例合集】【 1.3 识典古籍基于扣子智能助手的古籍数字化全流程技术解析】

古籍数字化智能处理系统全流程解析 本文详细介绍了基于扣子智能助手的古籍数字化解决方案,核心技术包括: 多层级架构设计:采用"云-边-端"三层次架构,整合高精度扫描设备与AI处理模块,实现从物理古籍到结构化数据的全流程自动化。 智能图像处理:通过12道预处理工序(包括畸变校正、污渍去除和对比度增强)处理古籍图像,采用改进的EAST算法和ViT模型实现高精度文字检测与识别,准确率达98.7%。 NLP增强处理:结合Transformer-XL预训练模型和N-gram语言模型进行文本校正,基于

商务合作|问题讨论|交流学习 请联系作者微信,加微信请务必注明来意,博客主页有联系方式 1451

106.Construct Binary Tree from Inorder and Postorder Traversal

题目描述(中等难度) 思路分析 可以先看一下 105 题,直接在 105 题的基础上改了,大家也可以先根据 105 题改一改。 105 题给的是先序遍历和中序遍历,这里把先序遍历换成了后序遍历。 区别在于先序遍历的顺序是 根节点 -> 左子树 -> 右子树。 后序遍历的顺序是 左子树 -> 右子树 -> 根节点。 我们当然还是先确定根节点,然后在中序遍历中找根节点的位置,然后分出左子树和右子树。 对于之前的解法一,传数组的两个边界,影响不大,只要重新计算边界就可以了。 解法一 常规

猫敷雪 361

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. 给定一个二叉树的后序和中序遍历,重建这棵二叉树。 此题和LeetCode105 根据前序和中序重建二叉树类似。 所谓后序遍历,即先访问根的左、右子树,然后再访问根节点。这样根节点在二叉树后序遍历的最后一个个元素。 所谓中序遍

哈哈的个人专栏 981

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal

Difficulty: 3 Frequency: 3 Problem: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Solution:

claviclelau的专栏 698

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode:106. Construct Binary Tree from Inorder and Postorder Traversal

题目的大体要求如下: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] p

Leo_Chen008的博客 295

Leetcode 106:Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,...

qq_34314736的博客 154

LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,...

liveway6的博客 137

嵌入式驱动“能跑”不等于“稳定”:量产级工程化实战指南

在嵌入式开发中,驱动代码在开发板上运行正常并不代表在量产环境下依然稳定。硬件差异、温度漂移、上电时序、中断竞争等边界条件往往是导致随机崩溃、外设挂死和数据错乱的根源。通过引入有界等待、状态校验、原子访问封装、错误诊断等工程化手段,可以将驱动的健壮性从“功能正确”提升至“状态可控”。本文从驱动崩溃的常见根因出发,结合超时机制、寄存器封装、崩溃现场采集和压力测试等实战方法,梳理样机到量产阶段的关键改造步骤,帮助驱动工程师建立边界意识,系统化解决“能跑但会崩”的难题,并为后续可维护性和可验证性打下基础。

weixin_33970449的博客 362

LeetCode106:Construct Binary Tree from Inorder and Postorder Traversal

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution {

BD的笔记 374

Leetcode做题日记:106. Construct Binary Tree from Inorder and Postorder Traversal(PYTHON)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,...

weixin_44033136的博客 247

python写算法题:leetcode: 106. Construct Binary Tree from Inorder and Postorder Traversal

class Solution(object): def constructTree(self, inorder, postorder, indMap, inind0, inind1, postind0, postind1): if inind0>=inind1: return None node=TreeNode(posto...

366

C++详解Leetcode:106. Construct Binary Tree from Inorder and Postorder Traversal

原题思路通过二叉树的中序遍历和后序遍历来构建二叉树,通过递归可以很简单的解决code/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),

楼上小宇___home 753

leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas

mikefan1991的博客 235

leetcode question 106 :Construct Binary Tree from Inorder and Postorder Traversal

问题:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, giveninorder = [9,3,15,20,7] postorder = [9,15,7...

csdn609387481的博客 186

leetcode: 106. Construct Binary Tree from Inorder and Postorder Traversal

Difficulty Medium. Problem Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9...

JNing 250
上一篇: LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
下一篇: LeetCode: Binary Tree Level Order Traversal II [107]
HarryHuang1990
博客等级 码龄16年 96粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值