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. drawableRightset 和 CompoundDrawables

    android:drawableRight="@drawable/check_down" 在代码中的用法是: Drawable drawable = getResources(). ...

  2. python学习【第九篇】python面向对象编程

    一.面向对象了解 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. Pyth ...

  3. 类与类之间关系,用C#和JavaScript体现

    前言 在面向对象中,类之间的关系有六种,分别是: 关联关系(Association) 泛化关系(Generalization) 依赖(Dependency) 聚合(Aggregation) 组合(Co ...

  4. linux磁盘清理

    一.背景: 1.由于linux系统空间是由挂载磁盘得来的,但有时装系统时挂载/根目录空间不大,现仅清除用户下载的大文件 二.方法: 1.输入命令df -h显示当前磁盘挂载(包含剩余空间)情况这里写图片 ...

  5. 深入C#学习系列一:序列化(Serialize)、反序列化(Deserialize)(转)

    序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方.    .NET框架提供了两种串行化的方式: ...

  6. ajax异步请求分页显示

    html代码: <!DOCTYPE html> <html lang="en"> <head>     <meta charset=&qu ...

  7. python函数回顾:abs()

    函数:abs() 官方英文文档解释 abs(x) Return the absolute value of a number. The argument may be a plain or long ...

  8. hadoop linux 杂记

    切换到root        su    修改sudo sudo + 命令 --> root权限 + 命令        su root        vim /etc/sudoers      ...

  9. text_field text_tag 用法

    = f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...

  10. iOS iPhone X 适配启动图片

    刚出了Xcode9正式版 迫不及待地下载 使用了 保密了这么久的iPhone X 模拟器 运行起来这个样子 上下都留白不正常 这必须匹配新的启动图才行,马上查苹果开发文档 get it!!!! 添加了 ...