FTPrep, 106 Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:音乐播放器源码 编辑:程序博客网 时间:2024/06/02 07:08
和105 是一个道理,套路是一样一样的。
代码:
/** * 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) { if(inorder.length==0 || postorder.length==0) return null; HashMap<Integer, Integer> indexInInorderMap= new HashMap<>(); for(int i =0; i<inorder.length; i++) indexInInorderMap.put(inorder[i], i); return constructTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1, indexInInorderMap); } private TreeNode constructTree(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight, HashMap<Integer, Integer> map){ if(inLeft>inRight || postLeft>postRight) return null; int rootIndexInInorder= map.get(postorder[postRight]); TreeNode root=new TreeNode(postorder[postRight]); root.left=constructTree(inorder, inLeft, rootIndexInInorder-1, postorder, postLeft, postLeft+rootIndexInInorder-inLeft-1, map); root.right=constructTree(inorder, rootIndexInInorder+1, inRight , postorder, postLeft+rootIndexInInorder-inLeft, postRight-1, map); return root; }}// 关键之处!!!重中之重,就是递归时候的 左右index,inorder里面的index的好划分,因为就是一刀切,分两边。容易错的是:preorder/postorder里的片段是根据inorder里的左右两边长度来划分的,所以!!一定要长度相等,一定长度相等!!
阅读全文
0 0
- FTPrep, 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
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]
- LeetCode 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
- [LeetCode 106]Construct Binary Tree from Inorder and Postorder Traversal
- leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal
- LeetCode---(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
- leetcode 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
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- Codeforces Round #439 A The Artful Expedient 博弈解释 +暴力写法
- 二分查找
- Hibernate的持久化对象状态
- python里使用正则的finditer()函数
- mysql服务器无法连接10055错误解决示例
- FTPrep, 106 Construct Binary Tree from Inorder and Postorder Traversal
- JAVA提高篇(17)--打印流PrintStream记录日志文件
- Codeforces869C The Intriguing Obsession
- SecureCRT8.0安装与破解
- 彻底理解js的对象继承
- 《软件测试的艺术》读书心得 --- 软件测试的心理学和经济学
- 监控内存瓶颈
- 使用construct2制作简单游戏
- 有重复元素的排列问题