已經(jīng)為此工作了幾個小時,但我似乎仍然無法提出一個可行的解決方案。11.13 二叉樹引導(dǎo)信息流 X279:二叉搜索樹小計數(shù)練習(xí)
1 回答

絕地?zé)o雙
TA貢獻1946條經(jīng)驗 獲得超4個贊
我的第一個答案沒有用。這個新的可以。
第一個問題是你實際上沒有計算任何東西,你只是遞歸地遍歷每個節(jié)點并檢查它的值。
其次,檢查也沒有按預(yù)期工作。您必須檢查是否應(yīng)計算當前節(jié)點。您還應(yīng)該進一步向下計算樹,因為這些節(jié)點可能小于鍵。
我的工作實施:
public int BSTsmallcount(BinNode root, int key)
{
int count = 0;
if (root == null) {
return 0;
}
else if (root.value() < key) {
count++;
count += BSTsmallcount(root.left(), key);
count += BSTsmallcount(root.right(), key);
}
else {
count += BSTsmallcount(root.left(), key);
}
return count;
}
添加回答
舉報
0/150
提交
取消