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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…