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.
思路:深度优先,和求二叉树最大深度类似,没什么好说的,打发时间的好题目~/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int dfs(TreeNode n, int depth){
if(n == null) return Integer.MAX_VALUE - 10;
if((n.left == null) &&(n.right==null)) return depth;
int l = dfs(n.left,depth + 1);
int r = dfs(n.right,depth+1);
return Math.min(l,r);
}
public int minDepth(TreeNode root) {
if(root == null) return 0;
return dfs(root,1);
}
}