Question: Can someone please check my recursive function below for mistakes? Thanks! Assignment: Write a function to find the sum of all the odd keys in
Can someone please check my recursive function below for mistakes? Thanks!
Assignment: Write a function to find the sum of all the odd keys in a Binary Search Tree.
public int numOddKeys(Node root) {
return numOddKeysHelper(root);
}
public int numOddKeysHelper(Node root) {
if(root == null) return 0;
int left = numOddKeysHelper(root.left);
int right = numOddKeysHelper(root.right);
if(root.data % 2 == 0) return left +right;
return (left + right + 1);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
