我的代码有问题。它显示:
BSTNode'<'Golfer'>'无法转换为BinarySearchTree'<'Golfer'>'。
请向我提供一些修复代码的提示。
public int countLowerScores(BinarySearchTree<Golfer> tree, int maxValue) {
BSTNode<Golfer> node = tree.root;
if (node == null) {
return 0;
}
int countLeft = countLowerScores(node.getLeft(), maxValue);
int countRight = countLowerScores(node.getRight(), maxValue);
return (node.getInfo() > maxValue ? 1 : 0) + countLeft + countRight;
}
答案 0 :(得分:0)
我不知道BinarySearchTree
和BSTNode
有什么用,但是您必须具有相同类型的节点,左子节点和右子节点。
这就是树的整个思想,它是递归的数据结构,这意味着可以用相同的方式处理根和子级。这是一个示例:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
方法:
public int countLowerScores(TreeNode root, int maxValue) {
if (root == null) {
return 0;
}
int countLeft = countLowerScores(root.left, maxValue);
int countRight = countLowerScores(root.right, maxValue);
return (root.val > maxValue ? 1 : 0) + countLeft + countRight;
}
您正在使用泛型使事情复杂化。
答案 1 :(得分:0)
您有使用.de {
display: none;
}
作为参数的countLowerScores
方法。现在您无法将节点传递给它。您可以使用BST遍历并集成每个节点并比较该值。
tree