Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
解法1: BFS, O(N),
典型的用BFS的题,按层遍历,只要找到一个为叶子的节点,则当前记录的层数一定是最小层。 用一个queue来完成BFS。
C++
用到了queue
Java
解法2: Recursive, Divide and Conquer, O(N)
一个数的最小层数是min(leftMin, rightMin) + 1, 用分治法和递归解决, code可以做到很简洁。
关于BFS和分治两者的复杂度分析,应该是一样的,参考九章的一个解答
C++