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 3
/ \ / \
4 5 6 7

后序遍历结果为:4 5 2 6 7 3 1

中序遍历结果为:4 2 5 1 6 3 7

由此可以发现规律:

1、后序遍历的最后一个字符,就是根结点(1)

2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;

3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。

代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return buildSubtree(postorder, inorder, ,
postorder.size()-,
, inorder.size()-);
} TreeNode* buildSubtree(vector<int>& postorder, vector<int>& inorder,
int p_left, int p_right, int i_left, int i_right) {
if (p_left > p_right || i_left > i_right)
return NULL; int root = postorder[p_right];
TreeNode* node = new TreeNode(postorder[p_right]); int range = ;
for (int j = i_left; j <= i_right; ++j) {
if (root == inorder[j]) {
range = j - i_left;
break;
}
} node->left = buildSubtree(postorder, inorder,
p_left, p_left + range - ,
i_left, i_left + range - );
node->right = buildSubtree(postorder, inorder,
p_left + range, p_right - ,
i_left + range + , i_right);
return node;
}
};

【Leetcode】【Medium】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

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

  3. 【题解二连发】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 ...

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

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

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

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

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

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

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

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

  10. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. es中的date类型

    JSON中没有date类型,es中的date可以由下面3种方式表示: ①格式化的date字符串,例如"2018-01-01"或者"2018-01-01 12:00:00& ...

  2. Restful API学习笔记

    之前关于这个概念在网上看了一些,看完似懂非懂,模模糊糊,发现专业术语或者说书面表达的形式对于理解这种十分抽象的概念还是低效了点. 书面文档方面看了以下几个: 理解本真的REST架构风格 1. 要深入理 ...

  3. RabbitMQ 很成熟 不是阿里的

    简介 官网 http://www.rabbitmq.com RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现 RabbitMQ实现了AMQ ...

  4. GridView控件详解

    一.介绍 GridView控件一表格形式显示数据源中的数据.提供对列进行排序.分页以及编辑.删除单个记录的功能. 二.绑定数据源 第一种使用DataSourceID属性.可以直接把GridView控件 ...

  5. ORA-XXXX错误集合

    第一.ORA-12514:listener does not currently know of service requested in connect descriptor 监听器巴拉巴拉一堆,然 ...

  6. ES6基本语法之let和const

    1.var可以重复声明 var a = 12; var a = 5; alert(a) //5 2.var无法限制修改 如:PI = 3.1415: 3.var没有块级作用域 { } 像: if(){ ...

  7. Hosts文件说明

    Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...

  8. EPPlus导入导出不占用进程

    导入: using (ExcelPackage package = new ExcelPackage(new FileStream(openFile.FileName, FileMode.Open, ...

  9. Magento 2中文手册教程 - Magento 2 安装流程图

    下图提供了安装Magento 2的安装流程概述: 设置你的服务器环境. 安装magento 2 必备软件, PHP, Apache, MySQL. 系统需求详细信息: 2.1.x 系统需求 获得mag ...

  10. [转] 多种方法查看Oracle SQL执行计划

    本文转自:http://falchion.iteye.com/blog/616234 一.在线查看执行计划表 如果PLAN_TABLE表不存在,执行$ORACLE_HOME/rdbms/admin/u ...