I'm using recursion to find the shortest path in a BST(Binary Search Tree) and the shortest path should be the first childless leaf that is found. Whenever I return it gives me back the root. I've tried many various ways and I either keep getting the root back for a nullPointerException. Here is what I have
public int minPath(){
if(isEmpty()){
return -1;
}
else{
return findMin(root);
}
}
private int findMin(IntegerTreeNode tNode){
if((tNode.left != null) && (tNode.right != null)){
findMin(tNode.left);
findMin(tNode.right);
}
return tNode.item;
}
I think what is happening is that it is returning the start of the stack, so how would I return the first childless leaf node?