根据一棵树的中序遍历与后序遍历构造二叉树。

注意:

你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

3 / \ 9 20 / \ 15 7

class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if(postorder.size() == 0)
return NULL;
if(postorder.size() == 1)
return new TreeNode(postorder[0]);
int iroot = postorder[postorder.size() - 1];
int ipos = 0;
for(int i = 0; i < inorder.size(); i++)
{
if(inorder[i] == iroot)
{
ipos = i;
break;
}
}
TreeNode *root = new TreeNode(iroot);
vector<int> v1(postorder.begin(), postorder.begin() + ipos);
vector<int> v2(inorder.begin(), inorder.begin() + ipos);
vector<int> v3(postorder.begin() + ipos, postorder.end() - 1);
vector<int> v4(inorder.begin() + ipos + 1, inorder.end());
root ->left = buildTree(v2, v1);
root ->right = buildTree(v4, v3);
return root;
}
};

Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树的更多相关文章

  1. LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9, ...

  2. 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树

    给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树:    ...

  3. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  4. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  5. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  6. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  7. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

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

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

随机推荐

  1. 创建你的 /proc 文件

    一旦你有一个定义好的 read_proc 函数, 你应当连接它到 /proc 层次中的一个入口项. 使用一个 creat_proc_read_entry 调用: struct proc_dir_ent ...

  2. 基于VGGnet的人脸识别系统-ubuntu 系统下的Caffe环境搭建(CPU)

    对于caffe的系统一般使用linux系统,当然也有windows版本的caffe,不过如果你一开始使用了windows下面的caffe,后面学习的过程中,会经常遇到各种错误,网上下载的一些源码.模型 ...

  3. uoj349 即时战略

    题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...

  4. CentOS7配置Docker镜像加速器

    1. 将默认的配置文件复制出来 cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service 2. 将加速器地 ...

  5. sqlite3-入门日记4-实现C++类封装

    一.前言:   今天试了下如何用C++类实现接口封装,感觉蛮好 .用于封装的类主要有两个,SQLiteStatement类和SQLiteWrapper类,是一个老外写的.我看了下源码,主要是对C接口进 ...

  6. Joomla - K2组件(文章管理扩展)

    一.下载 K2 进入 https://getk2.org/ ,点击DOWNLOAD K2 下载K2 下载完毕得到一个安装包 二.安装 K2 进入看后台,点击顶栏主菜单 扩展管理 -> 扩展安装 ...

  7. SPSS实例教程:多重线性回归,你用对了么

    SPSS实例教程:多重线性回归,你用对了么 在实际的医学研究中,一个生理指标或疾病指标往往受到多种因素的共同作用和影响,当研究的因变量为连续变量时,我们通常在统计分析过程中引入多重线性回归模型,来分析 ...

  8. Netty SimpleChannelInboundHandler和ChannelInboundHandler区别

    一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢? 在客 ...

  9. java日志管理 - slf4j+log4j2

    1 . 概述 1.1  日志框架实现 log4j是apache实现的一个开源日志组件: logback同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架,是slf4j ...

  10. vue 可复用swiper以及scoped样式穿透(可以不受scoped的限制来修改样式)

    参考: https://blog.csdn.net/dwb123456123456/article/details/82701740https://blog.csdn.net/u014027876/a ...