I am trying to make the method find the second smallest node However, when I found the smallest node(which doesn't have right node) I should return the parent of the node to make it "second" smallest. However, I don't have an idea to make it like that... please help me out guys
public StringNode secondSmallest() {
return secondSmallest(root);
}
private StringNode secondSmallest(StringNode x) {
if(x==null);
//base case: if the left node is null -> smallest
if (x.getLeft()==null) {
//if there is only right child
if(x.getRight()!=null) {
return x.getRight();
}
//when there is no right node and smallest
return x;
}
//keep finding left node
else
return secondSmallest(x.getLeft());
}