leetcode-111. Minimum Depth of Binary Tree
题目:
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; }
* }
*/
public class Solution {
private int dep = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
if(root==null) return 0;
count(root,1);
return dep;
}
private void count(TreeNode node,int d){
if(node == null) return ;
if(node.left==null && node.right==null) dep = Math.min(dep,d);
count(node.left,d+1);
count(node.right,d+1);
}
}