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.

 

 

 1 class Solution {
 2 public:
 3     int minDepth(TreeNode *root) {
 4         if(root==NULL) return 0;
 5         if(root->left==NULL&&root->right==NULL) return 1;
 6         int i=minDepth(root->left);
 7         int j=minDepth(root->right);
 8        
 9        if(i==0)
10             return j+1;
11         if(j==0)
12             return i+1;
13             
14         if(i<=j)
15             return i+1;
16         else
17             return j+1;
18     }
19 };

 

posted on 2015-05-10 07:46  黄瓜小肥皂  阅读(155)  评论(0编辑  收藏  举报