publicbooleanisBalanced(TreeNode root){ if (root==null) returntrue;
int leftTree = getHeight(root.left); int rightTree = getHeight(root.right); boolean res = Math.abs(leftTree-rightTree)<=1?true:false; if (res && isBalanced(root.left) && isBalanced(root.right)) { returntrue; } else { returnfalse; } }
privateintgetHeight(TreeNode node){ if (node == null) return0; int left = getHeight(node.left); int right = getHeight(node.right); return Math.max(left, right) +1; }