LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:组态王软件价格 编辑:程序博客网 时间:2024/06/08 06:41
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
这道题跟之前 Construct Binary Tree from Inorder and Preorder Traversal 思路完全一样,就是根节点和子树在数组中的位置变了。
代码如下:
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if(postorder == null || postorder.length == 0) return null; TreeNode root = construct(postorder, 0, postorder.length-1, inorder, 0, inorder.length-1); return root; } public TreeNode construct(int[] postorder, int pstart, int pend, int[] inorder, int istart, int iend){ if(pstart > pend) return null; TreeNode root = new TreeNode(postorder[pend]); if(pstart == pend) return root; int i; for(i=istart; i<=iend; i++){ if(inorder[i]==postorder[pend]) break; } root.left = construct(postorder, pstart, pstart+i-istart-1, inorder, istart, i-1); root.right = construct(postorder, pstart+i-istart, pend-1, inorder, i+1, iend); 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
- UltimateRecyclerView
- 常见的正则表达式总结
- 1 计算机组成原理和系统安装(云计算)
- UML建模快速入门00 Outline
- 如何规划和配置YARN和MapReduce 2
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- Sicily 13290 Play with Strings powered by wangbin
- HDU 5195 DZY Loves Topological Sorting
- 【LeetCode】Word Ladder 字符串
- virtual box下安装centos遇到的N个问题,纯新手为后来的更多纯新手发福利,少走弯路,少浪费时间!
- 习题2-7
- [LeetCode 39&40] Combination Sum I & II
- poj2985 并查集+树状数组第k小
- ZOJ - 1403 Safecracker