LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
题目:
Return any binary tree that matches the given preorder and postorder traversals.
Values in the traversals pre
and post
are distinct positive integers.
Example 1:
Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7]
Note:
1 <= pre.length == post.length <= 30
pre[]
andpost[]
are both permutations of1, 2, ..., pre.length
.- It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.
题解:
The first element in pre, 1 is actually the root's val.
The second element in pre, 2 is root's left child val. Find 2's index in post, before that, all are root left subtree.
Vice Versa.
Use a HashMap to store post element and its index for quick search.
In recursion, before using pre[preL+1], be careful with OutOfBoundException. Return when preL == preR. This makes sure after that, preL will not be out of index bound.
Time Complexity: O(n).
Space: O(n).
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public TreeNode constructFromPrePost(int[] pre, int[] post) { 12 if(pre == null || pre.length == 0 || post == null || post.length == 0){ 13 return null; 14 } 15 16 HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); 17 for(int i = 0; i<post.length; i++){ 18 hm.put(post[i], i); 19 } 20 21 return construct(pre, 0, pre.length-1, post, 0, post.length-1, hm); 22 } 23 24 private TreeNode construct(int[] pre, int preL, int preR, int[] post, int postL, int postR, HashMap<Integer, Integer> hm){ 25 if(preL > preR){ 26 return null; 27 } 28 29 TreeNode root = new TreeNode(pre[preL]); 30 if(preL == preR){ 31 return root; 32 } 33 34 int leftVal = pre[preL+1]; 35 int leftIndex = hm.get(leftVal); 36 root.left = construct(pre, preL+1, preL+1+leftIndex-postL, post, postL, leftIndex, hm); 37 root.right = construct(pre, preL+2+leftIndex-postL, preR, post, leftIndex+1, postR-1, hm); 38 return root; 39 } 40 }
类似Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11063066.html
LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal相关推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
- 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 ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- 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题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
最新文章
- Expo大作战(十八)--expo如何发布成独立应用程序,打包成apk或者ipa,发布到对应应用商店...
- oracle 事务故障,处理Oracle的分布式事务故障
- Window.navigator
- spring的Bean属性
- oracle数据泵导出csv文件,数据泵expdp导出遇到ORA-01555和ORA-22924问题的分析和处理...
- html盒子模型 1209
- python中a%b_Python中的a+=b和a=a+b之间的区别是什么?
- Discord是什么
- 您的账号与服务器断开连接,节奏大师您解决服务器断开连接的方法(帐号与服务器断开连接怎么办)...
- MAE:视觉自监督2021(原理+代码)
- weak和alias
- 锤子终究走起了小米的路子
- 慈航公益仲恺义工大区和爱心企业助力探亲日慈善活动
- 基于Echarts实现可视化数据大屏大数据可视化
- 7-2 高精度求累加和 分数 25作者 胡伟平单位 广西科技大学
- A Co-Memory Network for Multimodal Sentiment Analysis
- 笔记本显卡cpu 功耗测试软件,Alienware外星人X系列PK比51M系列性能,谁更强?应该选择买谁?笔记本显卡和CPU功耗如何?...
- python --安装pylab
- TK1上如何安装teamviewer,完结
- 解决Excel 2016无法直接打开文件的问题
热门文章
- 拳王虚拟项目公社:通过网络卖虚拟产品,月入1W的全自动化推广引流技术
- http/https 协议(概略)
- undo log mysql_MySQL的redo log与undo log
- python对csv文件中的数据进行分类_使用Python读取csv文件并进行分类存储
- python文本编辑器下载_海龟编辑器(Python编辑器)
- r语言和python爬虫谁厉害_R语言中,RCurl优势在哪儿,做爬虫的话用Python还是RCurl效率高?...
- java中序列化之子类继承父类序列化
- 【Flink】Flink 控制台能消费 但是 Flink 任务不能消费 内置topic 导致问题
- 95-38-035-Buffer-AbstractByteBuf
- 【Elasticsearch】Elasticsearch mapping 映射对象