boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - construct tree with preorder and inorder
相关主题
Construct Binary Tree from Preorder and Inorder Traversal算法复杂度?
Construct Binary Tree from Inorder and Postorder Traversal
讨论一道leetcode上面的题
construct bst from post and inorder 总是Memory Limit Exceeded
[leetcode] Binary Tree from Inorder & Postorder Traversal
问一道G家热题
Create Binary Tree from preorder and inorder arrays
这道题如何得到最优解
Amazon 三次电面面筋
Find the node with given value in binary tree in in-order
相关话题的讨论汇总
话题: 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版参与讨论
相关主题
Find the node with given value in binary tree in in-order
贡献几道G家onsite题
考大家个新题 visitor reconstruct generic tree
新鲜fb面经
Construct Binary Tree from Preorder and Inorder Traversal的setup 是不是有点问题?
请较一道面世题
全部答出来了,还是被amazon onsite据了
这个rebuild binary tree的问题
请教个G题目
L家电面(最新) 攒RP 求bless
相关话题的讨论汇总
话题: inorder话题: prel话题: preorder话题: int话题: inl