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.
这个算法的难点就是,要判断左边或右边是否为空,因为如果一边为空,它的深度肯定是小于另外一边,但是此时为空的算是没有深度了,所以最小深度要为另一边不为空的。大家看下我最开始写的算法,始终是超时,我真是百思不得其解。
public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null &a