[英]Counting nodes in a binary tree according to a given criteria (recursively)
I want to count the number of failed students who got a grade under 60 recursively using a binary search tree in java.我想使用 java 中的二叉搜索树递归计算成绩低于 60 的失败学生的数量。 I don't know if my method Is correct.
我不知道我的方法是否正确。 find the helper method to be used in the public wrapper method after
之后找到要在公共包装方法中使用的辅助方法
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 the if-condition that checking the current is null should return numoffailed
not numofpassed
如果检查电流的 if 条件是 null 应该返回
numoffailed
而不是numofpassed
I have updated the NumberFailStudents
to match the naming convention我已更新
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.