LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:抢购软件 编辑:程序博客网 时间:2024/05/01 02:38
题目
思路
1 很典型的面试题,和http://blog.csdn.net/xift810/article/details/19204495思路一样。
2 注意这里的边界条件,我的代码已经简化了。
代码
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder.length==0||postorder.length==0|| inorder.length!=postorder.length){ return null; } int length = inorder.length; return useme(inorder,0,length-1,postorder,0,length-1); } public TreeNode useme(int[] inorder,int istart,int iend,int[] postorder,int pstart,int pend){ TreeNode root = new TreeNode(postorder[pend]); int rootindex; for(rootindex = istart;rootindex<iend;rootindex++){ if(inorder[rootindex]==postorder[pend]){ break; } } int len = rootindex-istart; if(rootindex>istart){ root.left = useme(inorder,istart,rootindex-1,postorder,pstart,pstart+len-1); } if(rootindex<iend){ root.right = useme(inorder,rootindex+1,iend,postorder,pstart+len,pend-1); } return root; }}
0 0
- 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
- 【leetcode】【106】Construct Binary Tree from Inorder and Postorder Traversal
- Effective Objective-C(第15-22条)接口与API设计、深拷贝、浅拷贝
- 山东村民水井捞出女性头颅 死者系遭人肢解抛尸
- hdu4912
- CLOB字段的修改
- 开一个美甲店多少钱
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- C/C++:递归使用:使用递归反向写文件(例子)以及字符串翻转输出
- JAVA经典算法32题
- POJ 1442 Black box(单调队列求第k小数)
- 一个iOS 框架介绍:MKNetworkKit
- ubuntu 12.04 安装 openssh-server 失败,请问怎么该弄?
- 只言片语
- jsp + jquery + json
- android系统中Phone类型配置