/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicbooleanisBalanced(TreeNoderoot){if(root==null)returntrue;returnMath.abs(maxDepth(root.left)-maxDepth(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);}publicintmaxDepth(TreeNoderoot){if(root==null)return0;intleft=maxDepth(root.left);intright=maxDepth(root.right);returnleft>right?left+1:right+1;}}