// 返回的是高度差,-1的情况是高度差大于1的 publicintmaxDepth(TreeNode root){ if (root == null) { return0; } int left = maxDepth(root.left); int right = maxDepth(root.right); if (left == -1 || right == -1 || left - right > 1 || right - left > 1) { return -1; } if (left > right) { return left + 1; } return right + 1; } }