Question: Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This
Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception.
public class MyIntSET {
private Node root;
private static class Node {
public final int key;
public Node left, right;
public Node(int key) { this.key = key; }
}
public int sizeAboveDepth(int k) {
return abovehelp(root, k, 0);
}
public int abovehelp(Node x, int k, int count) {
int szl = abovehelp(x.left, k, count);
int szr = abovehelp(x.right, k, count);
if (x != null && count > k) {
count ++;
return 1 + szl + szr;
}
count ++;
return 0 + szl + szr;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
