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.
解法:用递归的思想,每次返回node点的最长深度,为max(left, right) + 1,然后从叶子节点向上返回,当node为null的时候返回0。
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return helper(root);
}
public int helper(TreeNode root) {
if (root == null) return 0;
int left = helper(root.left);
int right = helper(root.right);
return Math.max(left, right) + 1;
}