[LeetCode-20]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.
the idea is similar with the former one [Leetcode-21]
java
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildIP(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
public TreeNode buildIP(int[] inorder, int[] postorder, int i_s, int i_e, int p_s, int p_e){
if(p_s>p_e)
return null;
int pivot = postorder[p_e];
int i = i_s;
for(;i<=i_e;i++){
if(inorder[i]==pivot)
break;
}
TreeNode node = new TreeNode(pivot);
int lenRight = i_e-i;
node.left = buildIP(inorder, postorder, i_s, i-1, p_s, p_e-lenRight-1);
node.right = buildIP(inorder, postorder, i+1, i_e, p_e-lenRight, p_e-1);
return node;
}
c++
TreeNode *BuildTreeIP(
vector<int> &inorder,
vector<int> &postorder,
int i_s, int i_e,
int p_s, int p_e){
if(i_s > i_e) return NULL;
int pivot = postorder[p_e];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i] == pivot)
break;
}
int length1 = i-i_s;
int length2 = i_e-i;
TreeNode *node = new TreeNode(pivot);
node->left = BuildTreeIP(inorder, postorder, i_s, i-1, p_s, p_s+length1-1);
node->right = BuildTreeIP(inorder, postorder, i+1, i_e, p_e-length2, p_e-1);
return node; }
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return BuildTreeIP(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
}
[LeetCode-20]Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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 -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) 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 ...
- 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 ...
- 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 ...
- 【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 ...
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
随机推荐
- [CEOI2008]order --- 最小割
[CEOI2008]order 题目描述: 有N个任务,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数, ...
- [POJ2337]Catenyms
题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...
- zoj 2587 判断最小割的唯一性
算法: 先求出残量网络,计算出从src能够到的点集A,再求出能够到dst的点集B,如果所有点都被访问到了,那么割就是唯一的,即(A,B),否则(A,V-A)和(V-B,B)都是最小割. (注意因为割的 ...
- HDU 5699 货物运输 二分
货物运输 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5699 Description 公元2222年,l国发生了一场战争. 小Y负责领导工人运输物 ...
- Codeforces Round #294 (Div. 2)B - A and B and Compilation Errors 水题
B. A and B and Compilation Errors time limit per test 2 seconds memory limit per test 256 megabytes ...
- C#中List<T>是怎么存放元素的
Jeffrey Zhao在"你的字典里有多少元素?"一文中,提到了他在面试时问过的一个问题:List<T>是怎么存放元素?不幸的是,自己也回答不出来,只知道怎么用,却不 ...
- 21扩展IEnumerable<T>泛型接口自定义LINQ的扩展方法
LINQ方法实际上是对IEnumerable<TSource>的扩展,如图: 本篇自定义一个MyWhere方法,达到与Where相同的效果. 使用LINQ自带的Where方法 ...
- 5.跟我学solr---QueryResponseWriter具体解释
简单介绍 QueryResponseWriter是solr的一个插件,与上一章讲的SolrRequestHandler是配对的,用于定义solr查询结果的返回格式. 回到solr admin的查询页面 ...
- VirtualBox 快捷键
VirtualBox 的 Host 键是哪一个? 默认的 Host = Right Ctrl 组合键,意思是键盘上两个 “Ctrl”中右边的那个.键盘上是没有 “Right”这个键的,刚开始不明白,后 ...
- APACHE 2.2.8+TOMCAT6.0.14配置负载均衡
目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站,要达到以下要求: 1. Apache 做为 HttpServer ,后面连接多个 tomcat 应用实例,并进行负载均 ...