class Node{
Integer data;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return Objects.equals(data, node.data);
}
@Override
public int hashCode() {
return Objects.hash(data);
}
}
class ReturnData{
boolean isBalance;
int height;
public ReturnData(boolean isBalance, int height) {
this.isBalance = isBalance;
this.height = height;
}
}
private static ReturnData isBalanceTree(Node root) {
if(root == null){
return new ReturnData(true, 0);
}
ReturnData leftData = isBalanceTree(root.left);
ReturnData rightData = isBalanceTree(root.right);
return new ReturnData(leftData.isBalance && rightData.isBalance
&& Math.abs(leftData.height - rightData.height) <= 1, Math.max(leftData.height, rightData.height) + 1);
}