# leetcode: 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.

#### Solution and Precautions:

Way 1: DFS to traverse the whole tree and use a variable to keep the current minimum depth, when encountering a  less deep one (actually the minimum depth variable needs to be updated only when reaching a leaf node), update the minimum depth:

Way 2: Try a similar way to get the maximum depth of a binary tree as the following codes show. Note the commented line is a trap which is very easy to fall into. In this problem, we need a path from root to leaf, so if we encouter NULL, it is not necessary a valid path, e.g.,

```1
/
2
```

There is only one valid path 1->2. This is the difference.

#### Tips and Remarks:

(1) Two different solutions reflect two different thinkings: (1) recursive thinking, (2) dfs oriented:
(2) Also note the different way to handle NULL node in the second method, if it is an empty tree, we return 0, for other NULL Nodes, we need to return INT_MAX to indicate there is no valid path!

Written on April 20, 2013