简单解释一下把:
利用递归,咱先看看条件
1 . 左子树是平衡二叉树
2. 右子树是平衡二叉树
3. 左右子树的高度差 < 2
public boolean isright(Node head){
return f(head).isBalance;
}
public static class Returntable{
int hight = 0;
boolean isBalance ;
public Returntable(int hight , boolean isBalance){
this.hight = hight;
this.isbalance = isbalance;
}
}
public Returntable f(Node head){
if(head == null){
return new Returntable(0 , true);
}
Returntable l = f(head.left);
Returntable r = f(head.right);
boolean balance = l.isBalance && r.isBalance && Math.abs((l.hight - r.right) < 2);
int high = Math.max(l,hight , r.hight) + 1;
return new Returntable(high , balance);
}