[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 ...
随机推荐
- 让 Git 全局性的忽略 .DS_Store
让 Git 全局性的忽略 .DS_Store Mac 中每个目录都会有个文件叫.DS_Store, 用于存储当前文件夹的一些 Meta 信息.每次提交代码时,我都要在代码仓库的 .gitignore ...
- 【BZOJ】4720: [Noip2016]换教室
4720: [Noip2016]换教室 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1690 Solved: 979[Submit][Status ...
- Python编码规则
1. 命名规则 1.1 变量名.包名.模块名 变量名通常有字母.数字和下划线组成,且首字母必须是字母或下划线,并且不能使用python的保留字:包名.模块名通常用小写字母 1.2 类名.对象名 类名首 ...
- Codeforces Round #357 (Div. 2) A. A Good Contest 水题
A. A Good Contest 题目连接: http://www.codeforces.com/contest/681/problem/A Description Codeforces user' ...
- Git_多人协作
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...
- Apache参数的优化(转)
按照前面提到的版本问题,Apache可以直接使用2.0版本产品线.针对Apache的优化主要是针对httpd.conf的优化,当然还有其他地方,如果特别留意的话,网上常有专家惊呼“居然这么多人忽略xx ...
- How to Make Portable Class Libraries Work for You
A Portable Class Library is a .NET library that can be used (in binary form, without recompiling) on ...
- Latex Error cannot determine the size of graphic 报错的解决的方法
Latex Error cannot determine the size of graphic 报错的解决的方法 插入jpg文件老是会报错... 追究了半天,原来是编译的命令又问题,不应该使用 la ...
- Oracle 11g 错误:ORA-28002: the password will expire within 7 days 解决方法
ERROR:ORA-28002: the password will expire within 7 days 错误是提示password快过期了,有两个办法解决问题. 一. 改动已经报错用户的pas ...
- MVC无限级分类02,增删改查
继上一篇"MVC无限级分类01,分层架构,引入缓存,完成领域模型与视图模型的映射",本篇开始MVC无限级分类的增删改查部分,源码在github. 显示和查询 使用datagrid显 ...