LeetCode#104. Maximum Depth of Binary Tree

LeetCode#104 Maximum Depth of Binary Tree

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.

分析

这是一道典型的二叉树遍历题目,可以查看之前对于“树的遍历”的介绍。可以选择深度优先搜索递归法解决:

1
2
3
4
5
6
7
8
9
int depth(node)
return depth(node, 0)

int depth(node, depth)
if (node = null)
return depth

depth++
return max(depth(node.left, depth), depth(node.right, depth))