I need to find the minimum value in a tree of Strings that is NOT a Binary Search Tree recursively. I tried looking at some other questions like mine, but I couldn't figure out the answer.
I've figured out that I have to find the min of each of the subtrees, then compare it to the root, and return the minimum value. But I'm not sure how to actually write that.
This is the header:
public static Object min(TreeNode t){
}
EDIT: So what I've figured out so far is this
public static Object min(TreeNode t){
if(t == null){
return ______;
}
else if(min(t.getLeft().compareTo(min(t.getRight()) < 0){
if(min(t.getLeft()).compareTo(t) < 0){
return min(t.getLeft());
}
}
else if(min(t.getLeft().compareTo(min(t.getRight()) > 0){
if(min(t.getRight()).compareTo(t) < 0){
return min(t.geRight());
}
}
else{
return t;
}
}
I think I'm going in the right direction, but I'm not sure what fits in return statement in the null base case. Could someone help me understand what should go in the return statement and why? And if I'm doing this right? Thanks