【发布时间】:2021-04-06 17:12:52
【问题描述】:
我也尝试过更改三元表达式以产生等效结果:
is_balanced = (Math.abs(lh-rh)
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;
}
【问题讨论】:
-
这个表达式不需要三元运算符 Math.abs(lh-rh) > 1 ? false:true 只需使用 is_balanced = ! Math.abs(lh-rh) > 1
标签: java conditional-operator static-variables