LeetCode Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:梦幻西游物价软件 编辑:程序博客网 时间:2024/06/11 10:20
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
这道题与Construct Binary Tree from Preorder and Inorder Traversal 思路相似,不同之处在于这里的root在postorder的最有一位,其他都相同,由inorder找出分切点。
Time O(n), Space O(n).
Note:在recursion调用时postorder的index更新一定会用到(index-inL). 我刚开始很凑巧的写成了postR = index-1, 第一次循环恰巧相等,但之后就不对了。
AC Java:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder == null || postorder == null || inorder.length == 0|| postorder.length == 0 || 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 helper(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1, hm); } private TreeNode helper(int[] inorder,int inL,int inR, int[] postorder,int postL,int postR, HashMap<Integer, Integer> hm){ if(inL>inR || postL>postR){ return null; } TreeNode root = new TreeNode(postorder[postR]); int index = hm.get(root.val); root.left = helper(inorder, inL, index-1, postorder, postL, index-inL+postL-1, hm); //error root.right = helper(inorder, index+1, inR, postorder, index-inL+postL, postR-1, hm); return root; }}
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- 苦逼啊--qt for android 5.4.1 camera
- VS2015 Android-APP环境的配置
- hdu 3068 最长回文 【Manacher求最长回文子串,模板题】
- U盘安装centos7
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- eclipse中将Maven Dependencies Libraries移除后的恢复办法
- eclipse Maven 插件添加第三方远程仓库
- [Amazon ]去元音 get rid of vowels
- android不能直接连接mysql
- [Amazon]Given 2 numbers. Find if they are consecutive gray (grey) code sequences
- Eclipse使用Maven构建web项目二
- centos7安装vlc播放器
- poj 2985 The k-th Largest Group (并查集x全局动态第k大)