Below is the IntTree class we discussed in week 3 and that you worked on in HW3.  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 /* recursive case */ so that the leafCount method works correctly? return tempL + 1;   return tempL + tempR + 1;   return tempL + tempR;   return tempR + 1;

icon
Related questions
Question

Below is the IntTree class we discussed in week 3 and that you worked on in HW3.  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 /* recursive case */ so that the leafCount method works correctly?

return tempL + 1;

 

return tempL + tempR + 1;

 

return tempL + tempR;

 

return tempR + 1;

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