class{ publicbooleanisBalanced(TreeNode root){ if (root == null) returntrue; int left = height(root.left); int right = height(root.right); if (left == -1 || right == -1) returnfalse; if (Math.abs(left - right) <= 1) { returntrue; } returnfalse; }
Integer height(TreeNode root){ if (root == null) return0; int left = height(root.left); int right = height(root.right); if (left == -1 || right == -1) return -1; if (Math.abs(left - right) <= 1) return Math.max(left, right) + 1; else return -1; } }
近期评论