30 分钟 - Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:sql字符串截断怎么改 编辑:程序博客网 时间:2024/05/16 09:35
递归的出入口没有做好折腾了半天。
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
* Definition for binary tree
* 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 (postorder.length == 0) {
return null;
}
if (postorder.length != inorder.length) {
return null;
}
TreeNode result = populateTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
return result;
}
private TreeNode populateTree(int[] io, int iostart, int ioend, int[] po, int postart, int poend) {
if (io.length != po.length) {
return null;
}
if (iostart > ioend || postart > poend || ioend > io.length || poend > po.length) {
return null;
}
int v = po[poend];
TreeNode result = new TreeNode(v);
if (postart == poend) {
return result;
}
int idx = -1;
for (int i = 0; i < io.length; i++) {
if (io[i] == v) {
idx = i;
break;
}
}
result.left = populateTree(io, iostart, idx - 1, po, postart, postart + (idx - iostart - 1));
result.right = populateTree(io, idx + 1, ioend, po, postart + idx - iostart, poend - 1);
return result;
}
}
0 0
- 30 分钟 - Construct Binary Tree from Inorder and Postorder Traversal
- *(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
- 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
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Objective-c语法之字符串NSString
- 写在前面的话
- 安卓学习140502:android Intent机制详解
- 取消MyEclipse的自动更新updating
- 疯狂Android讲义2.1.3
- 30 分钟 - Construct Binary Tree from Inorder and Postorder Traversal
- 拓扑排序-模板
- MFC 消息映射机制剖析
- NO_36_Double-base palindromes
- 愤慨的小鸟-----小鸟群筹办动画
- 类的自动类型转换和强制类型转换
- cocos2dx3.0无法取消Armatrue骨骼动画中的补间效果问题的解决办法
- 1分钟 - Linked List Cycle II
- 大数问题 ZOJ Problem Set - 2001 Adding Reversed Numbers