I have also tried changing the ternary expression to produce the equivalent result:
is_balanced = (Math.abs(lh-rh)<=1) ? true:false;
static boolean is_balanced=true;
public static int balHeight(Node node) {
if(node==null) return 0;
int lh = balHeight(node.left);
int rh = balHeight(node.right);
if(Math.abs(lh-rh)>1) is_balanced = false;
**// ternary not working here
// is_balanced = Math.abs(lh-rh) > 1 ? false:true;**
return Math.max(lh,rh)+1;
}
The equivalent code would be is_balanced = Math.abs(lh - rh) > 1 ? false : is_balanced
.
(Or, without the ternary: is_balanced = is_balanced && Math.abs(lh - rh) <= 1
.)