原题链接:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/这道题和Construct
Binary Tree from Preorder and Inorder Traversal思路完全一样,如果有朋友不了解,请先看看Construct
Binary Tree from Preorder and Inorder Traversal哈。这里的区别是要从中序遍历和后序遍历中构造出树,算法还是一样,只是现在取根是从后面取(因为后序遍历根是遍历的最后一个元素)。思想和代码基本都是差不多的,自然时间复杂度和空间复杂度也还是O(n)。代码如下:public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder==null || postorder==null || inorder.length==0 || postorder.length==0)
{
return null;
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0;i<inorder.length;i++)
{
map.put(inorder[i],i);
}
return helper(inorder,postorder,0,inorder.length-1, 0, postorder.length-1,map);
}
private TreeNode helper(int[] inorder, int[] postorder, int inL, int inR, int postL, int postR, HashMap<Integer, Integer> map)
{
if(inL>inR || postL>postR)
return null;
TreeNode root = new TreeNode(postorder[postR]);
int index = map.get(root.val);
root.left = helper(inorder,postorder,inL,index-1,postL,postL+index-inL-1,map);
root.right = helper(inorder,postorder,index+1,inR,postR-(inR-index),postR-1,map);
return root;
}
这道题和Construct
Binary Tree from Preorder and Inorder Traversal是树中难度比较大的题目了,有朋友可能会想根据先序遍历和后序遍历能不能重新构造出树来,答案是否定的。只有中序便利可以根据根的位置切开左右子树,其他两种遍历都不能做到,其实先序遍历和后序遍历是不能唯一确定一棵树的,会有歧义发生,也就是两棵不同的树可以有相同的先序遍历和后序遍历,有兴趣的朋友可以试试举出这种例子哈。
分享到:
相关推荐
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
Construct Binary Tree from Inorder and Postorder Traversa.根据先序后续构建二叉树
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
lru缓存leetcode LeetCode_Note leetcode 个人笔记 ...[106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted
leetcode 跳跃 Algorithm 算法题解: 包括书籍算法, 程序员算法面试指南, 还有leetcode算法题 ...construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...
Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree ...
https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 2 题目描述 根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序...
Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳...
leetcode 不会LeetCode_492--构造矩形 对于 Web 开发人员来说,了解如何设计网页的大小非常重要。 因此,给定一个特定的矩形网页区域,您现在的工作是设计一个矩形网页,其长度 L 和宽度 W 满足以下要求: 您设计的...
leetcode 316 LeetCode Summary Exclusive Time of Functions: 栈 Friend Circles:DFS Print Binary Tree:二叉树 Maximal Square:DP Maximal Rectangle:单调栈(Histogram变形) Largest Rectangle in Histogram:...
Construct Optimal Binary Search Tree by Using Greedy Algorithm
1400. 构造 K 个回文字符串哈希表(碰巧跟题解思路一模一样)function canConstruct(s: string, k: number): bo
poj与leetcode 自述文件 介绍 本repo由鞠承佑、钟恺健、王少泽创建。 它在帐户SuanFaRuoji(算法弱鸡)下...└──889_construct_binary_tree_from_preorder_and_postorder_traversal └──zkj_python.py 我们的任务
No prior experience is required.Game Development with Construct 2 teaches you to create 12 different game projects from a variety of genres, including car racing and tower defense to platformer and ...
to construct binary codes for users and items such that the preference of users over items can be accurately preserved by the Hamming distance between their respective binary codes. By using two loss ...
//type of binary tree struct Node /* type of node*/ { DataType info; BinTree llink; BinTree rlink; }; BinTree createEmptyTree( void ); BinTree consBintree (DataType val, BinTree left, ...
这就是我在LeetCode中解决的所有问题。 解决问题后,我将更新此存储库。 此仓库中的所有解决方案都是用Java编写的。 您可以关注我的仓库,阅读我的代码,并为我提供更好的解决方案。 另外,欢迎指出我的解决方案中的...
leetcode安卓 construct_android_dev_skill_tree 构建android知识体系 算法数据结构 Java进阶 Kotlin 性能优化 -