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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
int depth1=0,depth2=0;
if(root==NULL){
return 0;
}
depth1=minDepth(root->left);
depth2=minDepth(root->right);
if(depth1==0){
return depth2+1;
}
if(depth2==0){
return depth1+1;
}
return min(depth1,depth2)+1;
}
};