Just compute the heights in one pass, and track the min and max as you go. Your confusion lies in the fact that your functions return a single value, but really you need to determine a pair of values. So, you can pass in, say, an object that has a field for both that you can store the results in, and return your results through that instead of through the return value of the function:
class MinMax {
public int min;
public int max;
}
void computeMinMaxHeight (Node root, MinMax minmax) {
// update the fields of minmax accordingly
}
A convenient way to initialize the fields of MinMax
might be:
class MinMax {
public int min = Integer.MAX_VALUE;
public int max = Integer.MIN_VALUE;
}
Or add some flag that indicates that it is uninitialized, so the values are filled in correctly for the first item.
Edit: You could also return an int[2]
array as Changgeng suggests; it just depends on what you find more semantically appropriate. Personally, I'd opt for something like MinMax
(Java doesn't really have a standard class for representing value ranges), plus passing the output parameter to the function saves you on object allocations, if that is significant.