[LC] 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 duplicates do not exist in the tree.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
Map<Integer, Integer> mymap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
mymap.put(inorder[i], i);
}
return helper(0, inorder.length - 1, 0, postorder.length - 1, postorder, mymap);
} private TreeNode helper(int inLeft, int inRight, int postLeft, int postRight, int[] postorder, Map<Integer, Integer> mymap) {
if (inLeft > inRight) {
return null;
}
TreeNode cur = new TreeNode(postorder[postRight]);
int index = mymap.get(postorder[postRight]);
int leftSize = index - inLeft;
// postRight for left just add up leftSize
cur.left = helper(inLeft, index - 1, postLeft, postLeft + leftSize - 1, postorder, mymap);
cur.right = helper(index + 1, inRight, postLeft + leftSize, postRight - 1, postorder, mymap);
return cur;
}
}
[LC] 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 Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
- 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树
[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...
- 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 ...
- 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】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 ...
随机推荐
- JavaScript之基于原型链的继承
本文介绍下js的OOP中的继承. 上图的要点为:Foo函数在创建时会自动生成内置属性prototype,而typeof Foo.prototype是object类型的. 上图的要点为:Foo.prot ...
- POJ 1860:Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22648 Accepted: 818 ...
- 覆盖(重写)&隐藏
成员函数被重载的特征(1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual 关键字可有可无. 覆盖是指派生类函数覆盖基类函数,特征是(1)不同的范围(分别位于 ...
- mysql not in 或 in 优化
在MySQL 中,not in 或in 优化思路, 利用left join 来优化,类似如下的查询方式: select id from a where id in (select id from b ...
- PHP使用redis防止大并发下二次写入
php调用redis进去读写操作,大并发下会出现:读取key1,没有内容则写入内容,但是大并发下会出现同时多个php进程写入的情况,这个时候需要加一个锁,即获取锁的php进程有权限写. $lock_k ...
- 题解 P1829 【[国家集训队]Crash的数字表格 / JZPTAB】
题目 我的第一篇莫比乌斯反演题解 兴奋兴奋兴奋 贡献一个本人自己想的思路,你从未看到过的船新思路 [分析] 显然,题目要求求的是 \(\displaystyle Ans=\sum_{i=1}^n\su ...
- Mac中制作USB系统启动盘
.iso镜像文件转 .dmg文件 hdiutil convert -format UDRW -o linuxmint.dmg ~/Desktop/linuxmint-19-cinnamon-64bit ...
- java实现图片和pdf添加铺满文字水印
依赖jar包 <!-- pdf start --> <dependency> <groupId>com.itextpdf</groupId> <a ...
- 后台用Hbase对表单数据实现增删改查遇到的问题
1.无法解析jsp 原因:hbase中lib下jar包会与tomcat包冲突,需要删除与tomcat冲突的包 这是我删除的几个包 之后运行就没有问题了 2.对于Hbase修改的问题 在添加数据时,HB ...
- Linux-进程状态和system函数
1.进程的5种状态 (1).就绪态. 这个进程当前所有运行条件就绪,只要得到CPU时间就能直接运行. (2).运行态 就绪态时得到了CPU就进入运行态开始运行. (3).僵尸态 进程已经结束但是父进程 ...