111. Minimum Depth of Binary Tree
problem description
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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
return its minimum depth = 2.
algorithm thought
得到最浅的叶子节点的深度,直接用广度优先搜索即可,广度优先搜索有找的极值的性质
code
algorithm analysis
如果用深度优先搜索做这个题目,最后时间复杂度会是O(n),因为会遍历所有节点,最后得到最浅的叶子节点。但是用广度优先搜索,只要找到第一个叶子节点就会返回,所以时间复杂度是降低很多的,不过应该也是O(n)
Last updated