Question

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

参考:http://www.cnblogs.com/zhonghuasong/p/7096300.html

Code

/**
* 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) {
if (inorder.size() == 0 || postorder.size() == 0)
return NULL;
return ConstructTree(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
} TreeNode* ConstructTree(vector<int>& inorder, vector<int>& postorder,
int in_start, int in_end, int post_start, int post_end) {
int rootValue = postorder[post_end];
TreeNode* root = new TreeNode(rootValue); if (in_start == in_end) {
if (post_start == post_end && inorder[in_start] == postorder[post_start])
return root;
} int rootIn = in_start;
while (rootIn <= in_end && inorder[rootIn] != rootValue)
rootIn++; int leftPostLength = rootIn - in_start;
if (leftPostLength > 0) {
// 注意起点和终点的写法
root->left = ConstructTree(inorder, postorder, in_start, rootIn - 1, post_start, post_start + leftPostLength - 1);
}
if (leftPostLength < in_end - in_start) {
root->right = ConstructTree(inorder, postorder, rootIn + 1, in_end, post_start + leftPostLength, post_end - 1);
} return root;
}
};

LeetCode——Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

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

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

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

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

  5. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

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

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

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

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

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

随机推荐

  1. LUA速成教程

    說明: 1.該教程適合對編程有一定了解的人員. 2.該教程在WINDOWS下實驗. 切入正題, 1.首先下載Notepad++,工欲善其事,必先利其器,然後安裝NotePad++的插件NppExec. ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记1.5

    这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...

  3. PHPFastCGI进程管理器PHP

    PHP-FPM是一个PHPFastCGI进程管理器,是只用于PHP的.      PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将它patch到你的PH ...

  4. SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)

    本节将会介绍SharePoint中最为常用的一些对象模型,以及如何使用这些对象模型来访问和操作网站中的数据.几乎所有的SharePoint服务器端开发都会涉及到这些内容,因此应着重掌握本节中所介绍的基 ...

  5. mesos cluster

    http://spark.apache.org/docs/latest/running-on-mesos.html http://stackoverflow.com/questions/1993985 ...

  6. python系列二:python3基本数据类型

    #标准数据类型——number(数字)a, b, c = 1, 2.2, "hello"print(a, end = ", ")print(b, end = & ...

  7. ORACLE中的MERGE语法使用记录

    项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...

  8. PS导出@3x、@2x、@1x格式的iOS切图神器-Retinize

    Retinize动作下载地址:http://retinize.it/ 使用:ps-载入动作-选中图片-执行动作

  9. JavaWeb 之过滤器

    1. 什么是过滤器 Servlet 是用来处理请求的, 过滤器是用来拦截请求的. 当用户请求某个 Servlet 时,会先执行部署在这个请求上的 Filter, 而 Filter 决定是否调用 Ser ...

  10. JavaScript中的Date,RegExp,Function对象

    Date对象 创建Date对象 //方法1:不指定参数var nowd1=new Date();alert(nowd1.toLocaleString( ));//方法2:参数为日期字符串var now ...