[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 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
Java:
public TreeNode buildTreePostIn(int[] inorder, int[] postorder) {
if (inorder == null || postorder == null || inorder.length != postorder.length)
return null;
HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>();
for (int i=0;i<inorder.length;++i)
hm.put(inorder[i], i);
return buildTreePostIn(inorder, 0, inorder.length-1, postorder, 0,
postorder.length-1,hm);
}
private TreeNode buildTreePostIn(int[] inorder, int is, int ie, int[] postorder, int ps, int pe,
HashMap<Integer,Integer> hm){
if (ps>pe || is>ie) return null;
TreeNode root = new TreeNode(postorder[pe]);
int ri = hm.get(postorder[pe]);
TreeNode leftchild = buildTreePostIn(inorder, is, ri-1, postorder, ps, ps+ri-is-1, hm);
TreeNode rightchild = buildTreePostIn(inorder,ri+1, ie, postorder, ps+ri-is, pe-1, hm);
root.left = leftchild;
root.right = rightchild;
return root;
}
Python:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
# @param inorder, a list of integers
# @param postorder, a list of integers
# @return a tree node
def buildTree(self, inorder, postorder):
lookup = {}
for i, num in enumerate(inorder):
lookup[num] = i
return self.buildTreeRecu(lookup, postorder, inorder, len(postorder), 0, len(inorder)) def buildTreeRecu(self, lookup, postorder, inorder, post_end, in_start, in_end):
if in_start == in_end:
return None
node = TreeNode(postorder[post_end - 1])
i = lookup[postorder[post_end - 1]]
node.left = self.buildTreeRecu(lookup, postorder, inorder, post_end - 1 - (in_end - i - 1), in_start, i)
node.right = self.buildTreeRecu(lookup, postorder, inorder, post_end - 1, i + 1, in_end)
return node if __name__ == "__main__":
inorder = [2, 1, 3]
postorder = [2, 3, 1]
result = Solution().buildTree(inorder, postorder)
print(result.val)
print(result.left.val)
print(result.right.val)
C++:
// Time: O(n)
// Space: O(n) /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
unordered_map<int, size_t> in_entry_idx_map;
for (size_t i = 0; i < inorder.size(); ++i) {
in_entry_idx_map.emplace(inorder[i], i);
}
return ReconstructPreInOrdersHelper(preorder, 0, preorder.size(), inorder, 0, inorder.size(),
in_entry_idx_map);
} // Reconstructs the binary tree from pre[pre_s : pre_e - 1] and
// in[in_s : in_e - 1].
TreeNode *ReconstructPreInOrdersHelper(const vector<int>& preorder, size_t pre_s, size_t pre_e,
const vector<int>& inorder, size_t in_s, size_t in_e,
const unordered_map<int, size_t>& in_entry_idx_map) {
if (pre_s == pre_e || in_s == in_e) {
return nullptr;
} auto idx = in_entry_idx_map.at(preorder[pre_s]);
auto left_tree_size = idx - in_s; auto node = new TreeNode(preorder[pre_s]);
node->left = ReconstructPreInOrdersHelper(preorder, pre_s + 1, pre_s + 1 + left_tree_size,
inorder, in_s, idx, in_entry_idx_map);
node->right = ReconstructPreInOrdersHelper(preorder, pre_s + 1 + left_tree_size, pre_e,
inorder, idx + 1, in_e, in_entry_idx_map);
return node;
}
};
类似题目:
[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
All LeetCode Questions List 题目汇总
[LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树的更多相关文章
- 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 ...
- 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 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(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- 【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 ...
随机推荐
- jquery选择器之全选择器
在CSS中,经常会在第一行写下这样一段样式 * {padding: 0; margin: 0;} 通配符*意味着给所有的元素设置默认的边距.jQuery中我们也可以通过传递*选择器来选中文档页面中的元 ...
- Mysql【第三课】
- debug版本的DLL调用release版本的DLL引发的一个问题
stl的常用结构有 vector.list.map等. 今天碰到需要在不同dll间传递这些类型的参数,以void*作为转换参数. 比如 DLL2 的接口 add(void*pVoid); 1.在DLL ...
- 项目Alpha冲刺--8/10
项目Alpha冲刺--8/10 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合 ...
- 2019-2020-1 20199301《Linux内核原理与分析》第九周作业
第八章 进程的切换和系统的一般执行过程 进程的调度实际与进程的切换 ntel定义的中断类型 硬中断:就是CPU的两根引脚(可屏蔽中断和不可屏蔽中断) 软中断/异常:包括除零错误.系统调用.调试断点等在 ...
- Windows10安装Redis过程
下载 Redis下载地址:https://github.com/microsoftarchive/redis/releases 以3.2.100版本为例,下载Redis-x64-3.2.100.zip ...
- 4、NameNode启动过程详解
NameNode 内存 本地磁盘 fsimage edits 第一次启动HDFS 格式化HDFS,目的就是生成fsimage start NameNode,读取fsimage文件 start Data ...
- rhcsa备战笔记
笔记全部手打 转载请加原文链接 0)重置密码开机按e 找到linux16行 rd.break console=tty0 ctrl+xmount -o remount,rw /sysrootchroo ...
- Jmeter+ant+jekins环境配置
Jmeter+ant+jekins 一.ant安装 1. ant安装 官网下载http://ant.apache.org 解压到想要的盘里面 2. 配置环境变量 (1)变量名:ANT_HOME 变量值 ...
- SQL必知必会收集学习
1.按查询列位置排序:如按第一列 降序排序 desc