由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - construct tree with preorder and inorder
相关主题
Construct Binary Tree from Preorder and Inorder Traversal算法复杂度?Amazon 三次电面面筋
Construct Binary Tree from Inorder and Postorder TraversalFind the node with given value in binary tree in in-order
讨论一道leetcode上面的题贡献几道G家onsite题
construct bst from post and inorder 总是Memory Limit Exceeded考大家个新题 visitor reconstruct generic tree
[leetcode] Binary Tree from Inorder & Postorder Traversal新鲜fb面经
问一道G家热题Construct Binary Tree from Preorder and Inorder Traversal的setup 是不是有点问题?
Create Binary Tree from preorder and inorder arrays请较一道面世题
这道题如何得到最优解全部答出来了,还是被amazon onsite据了
相关话题的讨论汇总
话题: inorder话题: prel话题: preorder话题: int话题: inl
进入JobHunting版参与讨论
1 (共1页)
C*******a
发帖数: 448
1
以下是这道题正确的代码
然而当我想把代码简洁一些,把标有*的4行换成1行:
int i = Arrays.asList(inorder).indexOf(preorder[preL]);
就会报错。谁知道错在哪儿???
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return _buildTree(preorder, 0, preorder.length, inorder, 0, inorder.
length);
}
private TreeNode _buildTree(int[] preorder, int preL, int preR, int[]
inorder, int inL, int inR) {
if (preL >= preR) {return null;}
TreeNode root = new TreeNode(preorder[preL]);
* int i = inL;
* for (; i < inR; i++) {
* if (inorder[i] == preorder[preL]) {break;}
* }
root.left = _buildTree(preorder, preL + 1, preL + 1 + i - inL,
inorder, inL, i);
root.right = _buildTree(preorder, preL + 1 + i - inL, preR, inorder,
i + 1, inR);
return root;
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
全部答出来了,还是被amazon onsite据了[leetcode] Binary Tree from Inorder & Postorder Traversal
这个rebuild binary tree的问题问一道G家热题
请教个G题目Create Binary Tree from preorder and inorder arrays
L家电面(最新) 攒RP 求bless这道题如何得到最优解
Construct Binary Tree from Preorder and Inorder Traversal算法复杂度?Amazon 三次电面面筋
Construct Binary Tree from Inorder and Postorder TraversalFind the node with given value in binary tree in in-order
讨论一道leetcode上面的题贡献几道G家onsite题
construct bst from post and inorder 总是Memory Limit Exceeded考大家个新题 visitor reconstruct generic tree
相关话题的讨论汇总
话题: inorder话题: prel话题: preorder话题: int话题: inl