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

    https://github.com/openresty/echo-nginx-module echo $echo_request_body

  2. 3dsmax2013卸载/安装失败/如何彻底卸载清除干净3dsmax2013注册表和文件的方法

    3dsmax2013提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2013失败提示3dsmax2013安装未完成,某些产品无法安装,也有时候想重新 ...

  3. 读取obj文件用Mesh创建实例化

    using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic; usin ...

  4. VirtualBox 命令行操作

    vboxmanage list vmsvboxmanage list runningvmsvboxmanage startvmvboxmanage controlvm "RHEL6.1_fo ...

  5. Java入门系列-15-封装

    为什么要封装 Student stu=new Student(); stu.age=-10; 上面的代码中 age 属性被随意访问,容易产生不合理的赋值 什么是封装 封装:将类的某些信息隐藏在内部,不 ...

  6. Oracle ORA-27102的解决办法(out of memory)

    原文出自:https://blog.csdn.net/seesun2012 Oracle ORA-27102:out of memory 错误解决办法(简单粗暴,100%能解决内存占用问题) -前置: ...

  7. jquery ui dialog弹出窗 清空缓存Cache或强制刷新

    我用jquery ui 弹出一个购物车的对话,通过AJAX加载的数据.发现购物车被缓存,一直看到是旧数据.为了刷新购物车更新,我必须去加一个刷新按钮,点击后更新购物车页面.有没有一种方法来自动刷新加载 ...

  8. Hadoop实战之三~ Hello World

    本文介绍的是在Ubuntu下安装用三台PC安装完成Hadoop集群并运行好第一个Hello World的过程,软硬件信息如下: Ubuntu:12.04 LTS Master: 1.5G RAM,奔腾 ...

  9. ,SQL语句关键词以及实例

    1.select:功能:查找,语法:select 列名 from 表名(注:可以一次从一个表中查询多个列或者从多个表名中查询资料) 实例:select Name from Table1,返回Table ...

  10. spring框架-----轻量级的应用开发框架

    一.bean 1.容器实例化 ApplicationContext ac=             new ClassPathXmlApplicationContext("applicati ...