由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 讨论一道leetcode上面的题
相关主题
Construct Binary Tree from Inorder and Postorder Traversal全部答出来了,还是被amazon onsite据了
[leetcode] Binary Tree from Inorder & Postorder Traversal大牛帮我看看这哪错了? iterative inorder traversal
construct bst from post and inorder 总是Memory Limit ExceededAmazon 电面
Construct Binary Tree from Preorder and Inorder Traversal算法复杂度?问几个有关Binary tree的题
这个inorder traversal 有错嘛,为什么leetcode 总报memory limit exceed?问一道算法题
construct tree with preorder and inorder问个1337网页上面的经典题
攒人品,amazon一面经历问一道二叉树遍历的问题? 谢谢!
Tree的traversal也分BFS和DFS?LeetCode题Binary Tree Inorder Traversal
相关话题的讨论汇总
话题: postorder话题: inorder话题: int话题: index话题: left
进入JobHunting版参与讨论
1 (共1页)
h********s
发帖数: 66
1
Given inorder and postorder traversal of a tree, construct the binary tree.
觉得并不难,但是online judge之后,judge small能通过,judge large不通过,报
Runtime Error。下面是我的代码,高人能否分析下问题出在哪儿了。
public static TreeNode buildTree(int[] inorder, int[] postorder) {
// Start typing your Java solution below
// DO NOT write main() function
if(inorder.length==0 && postorder.length==0) return null;
int n = inorder.length;
int root_val = postorder[n-1];
TreeNode root = new TreeNode(root_val);
int index=0;
for(int i=0;i if(inorder[i]==root_val) index=i;
}
int[] inorder_left = new int[]{};
if(index!=0) inorder_left= Arrays.copyOfRange(inorder, 0, index);
int[] inorder_right = new int[]{};
if(index!=n-1) inorder_right = Arrays.copyOfRange(inorder, index+1,
n);
int[] postorder_left = new int[]{};
if(index!=0) postorder_left = Arrays.copyOf(postorder, inorder_left.
length);
int[] postorder_right = new int[]{};
if(index!=n-1) postorder_right = Arrays.copyOfRange(postorder, index
,n-1);

root.left = buildTree(inorder_left,postorder_left);
root.right = buildTree(inorder_right,postorder_right);
return root;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
LeetCode题Binary Tree Inorder Traversal这个inorder traversal 有错嘛,为什么leetcode 总报memory limit exceed?
F家phone interview的一道题construct tree with preorder and inorder
一个电面疑问攒人品,amazon一面经历
LeetCode construct Binary TreeTree的traversal也分BFS和DFS?
Construct Binary Tree from Inorder and Postorder Traversal全部答出来了,还是被amazon onsite据了
[leetcode] Binary Tree from Inorder & Postorder Traversal大牛帮我看看这哪错了? iterative inorder traversal
construct bst from post and inorder 总是Memory Limit ExceededAmazon 电面
Construct Binary Tree from Preorder and Inorder Traversal算法复杂度?问几个有关Binary tree的题
相关话题的讨论汇总
话题: postorder话题: inorder话题: int话题: index话题: left