力扣-104-二叉树的最大深度

题目:
104. Maximum Depth of Binary Tree(easy)
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.
Note: A leaf is a node with no children.

Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.

题目大意:
给一个二叉树树根结点,找到其最大深度。

解题思路:
递归的典型例子,主要递归能实现的重要步骤。

代码:

1
2
3
4
5
6
7
Java:
public int maxDepth(TreeNode root){
if(root == null) return 0;
int length = Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
/* 这里的 +1 是递归实现的重要步骤 */
return length;
}