I have two methods to get min and max height of a binary tree in Java. But I am doing two traversals through the root two times. each is log n in Big(O)
. Is there a way to compute both min and max in same traversal and return as an array with two indices corresponding to min and max.
Here are my methods
public static int minHeight(Node root){
if (root==null) return -1;
return 1+Math.min(minHeight(root.left), minHeight(root.right));
}
public static int height(Node root){
if (root==null) return -1;
return 1+Math.max(height(root.left), height(root.right));
}
class Node {
Node left;
Node right;
int data;
public Node(int c){
this(c, null, null);
}
public Node(int c,Node left, Node right) {
this.data = c;
this.left=left;
this.right=right;
}
}