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 由中序和后序遍历建立二叉树的更多相关文章

  1. 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 ...

  2. 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: ...

  3. (二叉树 递归) 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 ...

  4. 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 ...

  5. 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 ...

  6. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. python测试开发django-rest-framework-61.权限认证(permission)

    前言 用户登录后,才有操作当前用户的权限,不能操作其它人的用户,这就是需要用到权限认证,要不然你登录自己的用户,去操作别人用户的相关数据,就很危险了. authentication是身份认证,判断当前 ...

  2. 独角兽估值30亿美金,我们聊聊RPA是什么

    https://www.jianshu.com/p/397ecd238ffc 缩短法定工作时间,已成国际劳动立法趋势,全球政府都曾面对这样的议题,过往企业IT也在思考这件事,开发出更好的软件系统帮助员 ...

  3. STM32启动代码详细分析

    最近需要学习iap的功能,因此离不开stm32的启动代码的分析,以前看了很多遍,都看不懂,读书百遍,其义自见,因此我有看了一遍,下面的文章,挺好的,因此转载: 在上电复位后,我们都知道会先运行启动代码 ...

  4. 使用go-mysql-server 开发自己的mysql server

    go-mysql-server是一个golang 的mysql server 协议实现包,使用此工具我们可以用来做好多方便的东西 基于mysql 协议暴露自己的本地文件为sql 查询 基于mysql ...

  5. video.js学习笔记

    video.js学习笔记获取用户观看时长

  6. WAMP 3.1.0 APACHE 2.4.27 从外网访问

    想测试一下从外网访问自己的电脑,找了一圈,网上教程都是修改APACHE 的 httpd.conf,经过1小时的摸索,发现完全不对. 正真的方法是修改httpd-vhost.conf,需要修改2处: 1 ...

  7. 微信小程序知识云开发

    一个小程序最多5个服务类目,一个月可以修改3次类目 小程序侵权投诉的发起与应对 软件著作权作品登记证书 实现小程序支付功能 如何借助官方支付api简单.高效率地实现小程序支付功能 借助小程序云开发实现 ...

  8. static final与final修饰的常量有什么不同

    最近重头开始看基础的书,对一些基础的概念又有了一些新的理解,特此记录一下 static final修饰的常量: 静态常量(static修饰的全部为静态的),编译器常量,编译时就确定其值(java代码经 ...

  9. 【CS224n】Lecture8 Notes

    注:这是2017年课程的lecture8.一直都在用RNN,但是对它内部的构造不甚了解,所以这次花了一个下午加一个晚上看了CS224n中关于RNN的推导,不敢说融会贯通,算是比以前清楚多了.做个笔记, ...

  10. Tomcat启动时,控制台和IDEA控制台中文乱码解决方案

    Tomcat启动时 控制台中文乱码 cmd控制台 IDEA控制台 解决方案 cmd乱码 打开Tomcat目录下的apache-tomcat-8.5.47\conf\logging.properties ...