由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这个BST题目为何错了?
相关主题
Finding deepest node of BST ?Google Front-end Software Engineer Phone Interview
一道题:2个BST,按大小顺序打印两棵树的所有节点bloomberg onsite题
BST insertionHow to find the kth biggest number in a BST
想到一道老题Lowest common ancestor of two nodes of Binary Tree
再上到题在版上看到的G题
largest bst 解法不理解的地方BST 找重复节点数
讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径recovery BST 不考虑相同值的情况么?
bst中序遍历c++ class iterator被问到primitive type和class type的区别
相关话题的讨论汇总
话题: value话题: rinsert话题: bstnode话题: node话题: 为何
进入JobHunting版参与讨论
1 (共1页)
i****1
发帖数: 445
1
public void insert (int value) {
rInsert (value, root);
}
private void rInsert (int value, BSTNode node) {
if (node == null) {
node = new BSTNode (value);
} else if (value < node.value) {
rInsert (value, node.left);
} else if (value > node.value) {
rInsert (value, node.right);
}
}
This approach will work in some programming
languages ... but not Java.
不知为为何上面的程序错了?我看挺好的,为何会出错。
而且答案是java里rinsert()函数需要返回值,如private BSTNode rInsert (int
value
, BSTNode node).
这时为何?
w***o
发帖数: 109
2
好像你没有把新的Node和BST链起来。你至少应该有象这样的操作:root.left = (
newly created node) or root.right = (newly created node)
好像任何Programming language都不应该work。你能说那个可以work吗?
x**********l
发帖数: 271
3
java pass argument by value, not reference

【在 i****1 的大作中提到】
: public void insert (int value) {
: rInsert (value, root);
: }
: private void rInsert (int value, BSTNode node) {
: if (node == null) {
: node = new BSTNode (value);
: } else if (value < node.value) {
: rInsert (value, node.left);
: } else if (value > node.value) {
: rInsert (value, node.right);

i********m
发帖数: 332
4
For every object that is not a primitive type, it's passed by reference.
Only the primitive type and their wrapper are passed by value.
for example :
public void foo (Integer i) {
i = 9;
}
public static void main (String[] args) {
Integer i = new Integer (10);
foo(i);
System.out.println(i);
//The value is still 10 in i, not 9
}
i****1
发帖数: 445
5
可是答案是要求函数增加一个返回值。其他的没变

【在 i********m 的大作中提到】
: For every object that is not a primitive type, it's passed by reference.
: Only the primitive type and their wrapper are passed by value.
: for example :
: public void foo (Integer i) {
: i = 9;
: }
: public static void main (String[] args) {
: Integer i = new Integer (10);
: foo(i);
: System.out.println(i);

g********s
发帖数: 125
6


【在 i********m 的大作中提到】
: For every object that is not a primitive type, it's passed by reference.
: Only the primitive type and their wrapper are passed by value.
: for example :
: public void foo (Integer i) {
: i = 9;
: }
: public static void main (String[] args) {
: Integer i = new Integer (10);
: foo(i);
: System.out.println(i);

g********s
发帖数: 125
7
java是pass by value no matter what. 我现在本科 刚学没多久 所以印象深刻。

【在 i********m 的大作中提到】
: For every object that is not a primitive type, it's passed by reference.
: Only the primitive type and their wrapper are passed by value.
: for example :
: public void foo (Integer i) {
: i = 9;
: }
: public static void main (String[] args) {
: Integer i = new Integer (10);
: foo(i);
: System.out.println(i);

1 (共1页)
进入JobHunting版参与讨论
相关主题
被问到primitive type和class type的区别再上到题
请教一个函数默认返回值的问题,纠结很久了largest bst 解法不理解的地方
java怎样解决pass by reference问题?讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径
请教一个OOP的C++问题bst中序遍历c++ class iterator
Finding deepest node of BST ?Google Front-end Software Engineer Phone Interview
一道题:2个BST,按大小顺序打印两棵树的所有节点bloomberg onsite题
BST insertionHow to find the kth biggest number in a BST
想到一道老题Lowest common ancestor of two nodes of Binary Tree
相关话题的讨论汇总
话题: value话题: rinsert话题: bstnode话题: node话题: 为何