If I want to find the smallest element in a tree that is bigger than an element x, would this be a correct way to do it?
class Node {
int data;
Node left, right;
}
Node root;
public Integer successorOf(int x) {
return successorOf(x, root);
}
private Integer successorOf(int x, Node n) {
if (n == null) {
return null;
}
if (x < n.data) {
Integer res = successorOf(x, n.left);
if (res == null)
res = n.data;
return res;
} else {
return successorOf(x, n.right);
}
}
I feel like this solution does not check the entire tree.
Help is much appreciated!