【LeetCode】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.

public class Solution {
    public int minDepth(TreeNode root) {
        int depth=0;
        if(root!=null){
             minPath(root,depth);
             return min;
        }
        return 0;
    }
    public int min = 0;
    private void minPath(TreeNode root, int depth) {
        if(root!=null){
            depth++;
            if(root.left==null&&root.right==null){
                if(min==0){
                    depth=min;
                }else{
                    if(min>depth)
                        min=depth;
                }
            }
            minPath(root.left,depth);
            minPath(root.right,depth);
            depth--;
        }
        
    }
    
}

 

posted @ 2014-05-11 16:04  一弦一仙  阅读(124)  评论(0编辑  收藏  举报