题目描述
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
代码
public static int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int nLeft = maxDepth(root.left);
int nRight = maxDepth(root.right);
return nLeft > nRight ? (nLeft + 1) : (nRight + 1);
}