题目描述

给出一棵树的中序遍历和后序遍历,请构造这颗二叉树
注意:
保证给出的树中不存在重复的节点

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

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


示例1

输入

复制

[2,1,3],[2,3,1]

输出

复制

{1,2,3}


//不需要辅助函数,简单易懂
//后序遍历容器的最后一个数是根节点,中序遍历的根节点左边是左子树,右边是右子树,
//后序遍历左子树节点值相邻,右子树节点值也相邻。由后序遍历最后一个值将中序遍历分成
//左右两部分,再由这两部分的size将后序遍历分成左右两部分,递归即可
 
class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        if(inorder.empty())
            return NULL;
        int nodeval=postorder[postorder.size()-1];
        TreeNode* head=new TreeNode(nodeval);
        vector<int>::iterator iter=find(inorder.begin(),inorder.end(),nodeval);
        vector<int>leftin=vector<int>(inorder.begin(),iter);
        vector<int>rightin=vector<int>(iter+1,inorder.end());
        int left=leftin.size();
        int right=rightin.size();
        if(left>0)
        {
            vector<int>leftpo=vector<int>(postorder.begin(),postorder.begin()+left);
            head->left=buildTree(leftin,leftpo);
        }
        if(right>0)
        {
            vector<int>rightpo=vector<int>(postorder.begin()+left,postorder.begin()+left+right);
            head->right=buildTree(rightin,rightpo);
        }
        return head;
    }
};

leetcode 43:construct-binary-tree-from-inorder的更多相关文章

  1. (二叉树 递归) 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 ...

  2. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

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

  4. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  5. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

  7. leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. C#解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 ...

  9. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  10. 【leetcode】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 ...

随机推荐

  1. C# 生成chart图表的三种方式

    .net中,微软给我们提供了画图类(system.drawing.imaging),在该类中画图的基本功能都有.比如:直线.折线.矩形.多边形.椭圆形.扇形.曲线等等,因此一般的图形都可以直接通过代码 ...

  2. Redis 客户端 Jedis、lettuce 和 Redisson 对比

    Redis 支持多种语言的客户端,下面列举了部分 Redis 支持的客户端语言,大家可以通过官网查看 Redis 支持的客户端详情. C语言 C++ C# Java Python Node.js PH ...

  3. 多测师讲解jmeter _接口请求_(003)高级讲师肖sir

    1.简单接口的请求 2. 3. 正则查看: 正则提取:在后置处理器中正则请求 设置:正则表达式 JSESSIONID提取器: Debug  sampler 总结:

  4. Rust之路(0)

    Rust--一个2012年出现,2015年推出1.0版本的"年轻"语言.在 2016 至 2018 年的 stack overflow 开发人员调查中,被评比为 "最受欢 ...

  5. day17 Pyhton学习 内置函数继续

    1. locals 本地作用域/局部作用域  会随着位置的改变而改变 2.globals 全局作用域   永远不变,永远是全局 3.complex:复数 实数(有理数和无理数) 某一个数的平方是-1 ...

  6. react中 受控组件和 非受控组件 浅析

    一 受控组件 顾名思义,受控 也就是能够被控制,简而言之也就是 该组件ui的显示或者内部state逻辑的变化依赖外部的 props的传入. 二 非受控组件 顾名思义,非受控,也就是内部的视图变化,st ...

  7. 习题3-3 数数字(Digit Counting , ACM/ICPC Danang 2007, UVa1225)

    #include<stdio.h> #include<string.h> int main() { char s[100]; scanf("%s",s); ...

  8. 实时离线一体化在资产租赁saas服务中使用

    流水查询需求 需求第一期: 基于TB级的在线数据,支持缴费帐单明细在线查询.大家都知道,像银行帐单流水一样,查几年的流水是常有的事. 支持的维度查询:帐期.欠费状态.日期范围.费用科目类型.房屋分类. ...

  9. 面试官:如何写出让 CPU 跑得更快的代码?

    前言 代码都是由 CPU 跑起来的,我们代码写的好与坏就决定了 CPU 的执行效率,特别是在编写计算密集型的程序,更要注重 CPU 的执行效率,否则将会大大影响系统性能. CPU 内部嵌入了 CPU ...

  10. flask生产环境部署

    1.安装uwsgipip install uwsgi 2.创建ini配置文件vim uwsgi.ini内容如下:[uwsgi]# 配置启动的服务地址和iphttp=0.0.0.0:5001# 项目目录 ...