思路:二叉树的最大深度= max{ 左子树的最大深度,右子树的最大深度} + 1
Java代码如下:
public class Solution {
// 二叉树的最大深度等于max {左子树的最大深度,右子树的最大深度} + 1
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}