【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
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 duplicates do not exist in the tree.
根据定义,后序遍历postorder的最后一个元素为根。
由于元素不重复,通过根可以讲中序遍历inorder划分为左子树和右子树。
递归下去即可求解。
/**
* Definition for binary tree
* 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 Helper(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
TreeNode* Helper(vector<int>& inorder, int begin1, int end1, vector<int>& postorder, int begin2, int end2)
{
if(begin1 > end1)
return NULL;
else if(begin1 == end1)
return new TreeNode(inorder[begin1]); TreeNode* root = new TreeNode(postorder[end2]);
int i = begin1;
for(; i <= end1; i ++)
{
if(inorder[i] == postorder[end2])
break;
}
int leftlen = i-begin1; root->left = Helper(inorder, begin1, begin1+leftlen-, postorder, begin2, begin2+leftlen-);
root->right = Helper(inorder, begin1+leftlen+, end1, postorder, begin2+leftlen, end2-);
return root;
}
};

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- LeetCode OJ 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 ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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: ...
- 【LeetCode】105 & 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 ...
- 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 ...
- (二叉树 递归) 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 ...
- [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 ...
随机推荐
- IOS http 请求
asihttprequest 为第三方数据请求,一下为get 和post 两种请求. Get: NSString *loginName=@"Tony"; NSString *pw ...
- linux基础命令学习(三)文件搜索 find
1.使用name选项 查找自己的根目录$Home中的文件,可以用: find ~ -name "*.log" -print 查找当前目录下的文件,可以用: find . -nam ...
- mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)
写在前面 MyBatsi 的拦截器模式是基于代理的代理模式.并且myBatis 的插件开发也是以拦截器的形式集成到myBatis 当中. MyBatis 的拦截器已经插件是在org.apache.ib ...
- Java是对象引用按值传递的
原因:JAVA核心编程1卷7版P116 Employee E1 = new Employee("Alice",...); Employee E2 = new Employee(&q ...
- Tasker, Android系统增强神器, 变量汇总
http://tasker.dinglisch.net/userguide_summary.html#variables.html http://tasker.dinglisch.net/usergu ...
- Gulp插件less的使用
1.创建:gulpfile.js var gulp = require('gulp'), less = require('gulp-less'); gulp.task('default', funct ...
- scp遇到路径中有空格
sudo scp root@1.1.1.1:/test/soft/123/Microsoft SQL Server 2000.iso . 错误! sudo scp root@1.1.1.1: ...
- JS中实现字符串和数组的相互转化
早上起来看了一道js的面试题,是这样描述的:利用var s1=prompt("请输入任意的字符串","")可以获取用户输入 的字符串,试编程将用户输入的字符串“ ...
- Apache Mahout 简介 通过可伸缩、商业友好的机器学习来构建智能应用程序
在信息时代,公司和个人的成功越来越依赖于迅速有效地将大量数据转化为可操作的信息.无论是每天处理数以千计的个人电子邮件消息,还是从海量博客文章中推测用户的意图,都需要使用一些工具来组织和增强数据. 这其 ...
- HTML学习要点
目标 掌握HTML基本语法,了解HTML Document结构,能熟练使用HTML Element对象. 要点 基本概念:什么是HTML.HTML标签? 熟悉常用的HTML标签含义以及应用场合. ht ...