[英]Counting nodes in a binary tree according to a given criteria (recursively)
我想使用 java 中的二叉搜索樹遞歸計算成績低於 60 的失敗學生的數量。 我不知道我的方法是否正確。 之后找到要在公共包裝方法中使用的輔助方法
private int NumberfailStudents(int numoffailed, BNode current) {
// BaseCase
if (current == null) {
return 0 + numofpassed;
} else {
if (current.data.getGrades() < 60) {
return ++numoffailed;
}
if (current.left != null) {
return NumberfailStudents(numoffailed, current.left);
}
if (current.right != null) {
return NumberfailStudents(numoffailed, current.right);
}
}
return numoffailed;
}
如果檢查電流的 if 條件是 null 應該返回numoffailed
而不是numofpassed
我已更新NumberFailStudents
以匹配命名約定
private int NumberFailStudents(int numOfFailed, BNode current) {
if (current == null)
return numOfFailed;
if (current.data.getGrades() < 60)
++numOfFailed;
if (current.left != null)
numOfFailed = NumberFailStudents(numOfFailed, current.left);
if (current.right != null)
numOfFailed = NumberFailStudents(numOfFailed, current.right);
return numOfFailed;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.