divided-and-conquer-106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:网络推手兼职平台 编辑:程序博客网 时间:2024/06/03 22:47
/** * 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 == null || inorder.length == 0) return null; //建立一个hash表,记录inorder数组的值->索引的映射 Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0; i < inorder.length; i++){ map.put(inorder[i],i); } return helper(inorder,0,inorder.length-1,postorder,0,postorder.length-1,map); } public TreeNode helper(int[] inorder,int is,int ie,int[] postorder,int ps,int pe,Map<Integer,Integer> map){ if(is > ie || ps > pe) return null; //取出根节点 TreeNode root = new TreeNode(postorder[pe]); //获取当前根节点的值在中序数组的索引,并将数组分为两部分 int index = map.get(postorder[pe]); //divided TreeNode left = helper(inorder,is,index-1,postorder,ps,ps+index-is-1,map); TreeNode right = helper(inorder,index+1,ie,postorder,ps+index-is,pe-1,map); //conquer root.left = left; root.right = right; return root; }}
阅读全文
0 0
- divided-and-conquer-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
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- codevs 1183 泥泞的道路 最长路 解题报告
- Git使用
- 求最大回文子串(马拉车算法)
- tablelayout+dreamlayout+Xlistview+lunbo
- recycleview的头布局和尾部局
- divided-and-conquer-106. Construct Binary Tree from Inorder and Postorder Traversal
- 深入理解JVM之虚拟机性能监控与故障处理工具
- WebService CXF框架的使用(实现JSON与XML数据传输)
- CodeVS1183 泥泞的道路 解题报告【二分答案+SPFA最长路】
- AndroidManifest主题设置
- c++运算符重载、类型转换函数总结
- 7.4 缓存的处理步骤
- TabLayout
- 按键精灵脚本笔记 鼠标按直线轨迹移动