這種方法有什麼問題?看來,但我不確定樹中鄰近兒童的比較沒有發生。沒有發生樹中相鄰兒童的比較?
我粗略地追溯到該算法通過手的工作,我認爲這個想法是正確的,也許有點毛病執行或我不知道如何遞歸工作,第二個輔助(比較)方法似乎是問題
public static int MAX(BST B) {
int m = ((Integer) B.root.data).intValue();
return call(B.root, m);
}
public static int call(node current, int max) {
//first helper method gets the max from two different levels in the tree
if(current == null)
return -1;
if(current.left == null && current.right == null)
return max;
else {
if(((Integer) current.data).intValue()>max)
max = ((Integer) current.data).intValue();
return compare(call(current.left,max),call(current.right,max));
}
}
//second helper method gets the max
static int compare(int m1, int m2) {
if(m1>m2)
return m1;
else
return m2;
}
添加語言標籤。 – Jon 2011-12-13 22:58:55