Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.
同样是给出了不会有重复数字的条件,用递归较容易实现,代码如下:

 /**
* 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() == )
return NULL;
return createTree(inorder, , inorder.size() - , postorder, , postorder.size() - );
} TreeNode* createTree(vector<int> & inorder, int inBegin, int inEnd,
vector<int> & postorder, int postBegin, int postEnd)
{
if(inBegin > inEnd) return NULL;
int rootVal = postorder[postEnd];
int mid;
for(int i = inBegin; i <= inEnd; ++i){
if(inorder[i] == rootVal){
mid = i;
break;
}
}
int len = mid - inBegin;
TreeNode * left = createTree(inorder, inBegin, mid - , //边界条件同样应该注意
postorder, postBegin, postBegin + len - );
TreeNode * right = createTree(inorder, mid + , inEnd,
postorder, postBegin + len, postEnd - );
TreeNode * root = new TreeNode(rootVal);
root->left = left;
root->right = right;
return root;
}
};

java版本的代码如下所示,没有区别:

 public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0)
return null;
return createTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
} public TreeNode createTree(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd){
if(inEnd < inBegin)
return null;
int rootVal = postorder[postEnd];
int mid = 0;
for(int i = inBegin; i <= inEnd; ++i){
if(inorder[i] == rootVal){
mid = i;
break;
}
}
int len = mid - inBegin;
TreeNode leftNode = createTree(inorder, inBegin, mid - 1, postorder, postBegin, postBegin + len - 1);
TreeNode rightNode = createTree(inorder, mid + 1, inEnd, postorder, postBegin + len, postEnd - 1);
TreeNode root = new TreeNode(rootVal);
root.left = leftNode;
root.right = rightNode;
return root;
}
}

LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)的更多相关文章

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

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

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

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

  4. (二叉树 递归) leetcode 106. 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] 106. 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 ...

  6. LeetCode 106. 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 ...

  7. C#解leetcode 106. 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 ...

  8. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

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

  10. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

随机推荐

  1. Oracle学习笔记—数据库,实例,表空间,用户、表之间的关系

    之前一直使用的关系型数据库是Mysql,而新公司使用Oracle,所以最近从网上搜集了一些资料,整理到这里,如果有不对的地方,欢迎大家讨论. 基本概念: 数据库:Oracle 数据库是数据的物理存储. ...

  2. [笔记]一道C语言面试题:IPv4字符串转为UInt整数

    题目:输入一个IPv4字符串,如“1.2.3.4”,输出对应的无符号整数,如本例输出为 0x01020304. 来源:某500强企业面试题目 思路:从尾部扫描到头部,一旦发现无法转换,立即返回,减少无 ...

  3. val() attr('value')

    val() 只能更改输入框内的值,能更改value属性, 在浏览器中体现不出value被改变 attr('value') 都可以 谷歌浏览器 val,attr都能获取输入框最新的value值

  4. iOS11 仿大标题 导航栏

    iOS11 SytleTitleController  仿大标题 风格 导航栏 仿 iOS11 大导航标题 风格 UI 适用范围 iOS8 + 前言 iOS11全面应用大标题设计,(岂止于大—— 比逼 ...

  5. 单口双线PC连接转换器 手机电脑耳机转接线

    看着标题是不是很绕, 其实这个需求我相信不少人都有, 只是可能很少会想到. 手机换了一个又一个, 佩戴的耳机同样是一个又一个, 最别扭的是, 用手机的时候往往不用耳机, 不少童鞋都会选择把手机的耳机放 ...

  6. Raspberry Pi开发之旅-WIFI遥控小车

    一.简单介绍树莓派的GPIO口 上图是树莓派2代的接口(不同型号接口会有差异),我们就以此为例来说下这些接口. 1.GPIO介绍 GPIO 英文全称是:General-purpose input/ou ...

  7. STM32系列第15篇--灵活的静态存储控制器FSMC

    源: STM32系列第15篇--灵活的静态存储控制器FSMC

  8. Redis之数据存储结构

    今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...

  9. Bürkert 流体控制系统 (8611 型通用调节器)

    Type Description High-Tech Made EasyThe new universal controller eCONTROL Type 8611 brings an essent ...

  10. 数独C语言算法

    备好:http://blog.chinaunix.net/uid-26456800-id-3380612.html