public class Solution {
    public static boolean flag = true;

    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null){
            flag = true;
        }
        isBalanced(root);
        return flag;
    }

    public int isBalanced(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = isBalanced(root.left);
        int right = isBalanced(root.right);
        if(left == -1 || right == -1 || Math.abs(left - right) > 1){
            flag = false;
            return -1;
        }
        return Math.max(left, right) + 1;
    }
}