https://leetcode.com/problems/balanced-binary-tree/
判断一个树是不是平衡二叉树
左右子树高度差小于等于一 && 左右子树都是平衡二叉树
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
private int height(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(height(root.left), height(root.right)) + 1;
}
}