Below is the IntTree class. We are in the middle of implementing a new recursive method called leafCount that should return the number of leaves in the tree.  What code should replace the comment /* base case 1 */ so that the leafCount method works correctly? return 0;   return null;   return 1;   throw new RuntimeException("error");

icon
Related questions
Question

Below is the IntTree class. We are in the middle of implementing a new recursive method called leafCount that should return the number of leaves in the tree.  What code should replace the comment /* base case 1 */ so that the leafCount method works correctly?

return 0;

 

return null;

 

return 1;

 

throw new RuntimeException("error");

 

public class IntTree {
private Node root;
}
private static class Node {
public int key;
public Node left, right;
}
}
public Node(int key) {
this.key = key;
public int leafCount() {
return leafCountH(root);
}
}
private int leafCountH (Node n) {
if (n == null) {
/* base case 1 */;
} else if (n.left == null & n.right
/* base case 2 */;
} else {
int templ =
leafCountH(n.left);
int tempR leafCountH(n.right);
/* recursive case */;
== null) {
Transcribed Image Text:public class IntTree { private Node root; } private static class Node { public int key; public Node left, right; } } public Node(int key) { this.key = key; public int leafCount() { return leafCountH(root); } } private int leafCountH (Node n) { if (n == null) { /* base case 1 */; } else if (n.left == null & n.right /* base case 2 */; } else { int templ = leafCountH(n.left); int tempR leafCountH(n.right); /* recursive case */; == null) {
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer