[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 ...
随机推荐
- SSH项目整合基本步骤
SSH项目整合基本步骤 一.项目简介 该项目是由Spring4.Struts2 以及 Hibernate4 整合搭建的 web 项目,把Action分开编写,便于查看,使用JSTL.EL标签. 二.项 ...
- java线程本地变量
ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许把它命名为Thre ...
- wxwidget wxpython 可视化开发工具
wxwidget官方建议的工具集合:http://wiki.wxwidgets.org/Tools 支持wxpython可视化开发工具 wxFormBuilder wxGlade wxDesigner ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #256 (Div. 2) E Divisors
E. Divisors Bizon the Champion isn't just friendly, he also is a rigorous coder. Let's define functi ...
- CentOS 7修改网卡名为eth0
第一步: 编辑文件加入如下所示参数 vi /etc/sysconfig/grub GRUB_CMDLINE_LINUX=”rd.lvm.lv=vg0/swap vconsole.keymap=us c ...
- THE CUSTOMISER
http://www.wanga.com/cu.php The Customiser incorporates all of the features of Magic Mouse. It also ...
- js删除字符串的最后一个字符三种方法
字符串 var basic = "abc,def,ghi,"; 第一种 basic = basic.substr(0, basic.length - 1); 第二种 basic = ...
- linux下安装MYSQL详细配置(转)
#tar zxvf mysql-5.0.18.tar.gz#cd mysql-5.0.18 #./configure --prefix=/usr/local/mysql --with-chars ...
- 分布式中使用redis进行session共享
摘要 在asp.net web中,session经常用来存储当前用户信息,或者通过session进行登录权限的验证.如果是一台服务器,session的使用没问题,如果使用nginx等实现反向代理,将站 ...