java版平衡树,是每个节点的左子树和右子树深度不超过1,所以需要遍历每个节点,并且得到左右子树的深度

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null) return true;
        //每个节点的左子树深度
        int l = dfs(root.left);
        //每个节点的右子树深度
        int r = dfs(root.right);
        return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right) && Math.abs(l - r) <= 1; 
    }
    public int dfs(TreeNode t){
        if(t == null){
            return 0;
        }
        return Math.max(dfs(t.left),dfs(t.right)) + 1;
    }
}