/**
* 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) {
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left&&root->right==NULL)
return minDepth(root->left)+1;
else if(root->right&&root->left==NULL)
return minDepth(root->right)+1;
else if(root->left&&root->right)
{
int left=minDepth(root->left);
int right=minDepth(root->right);
return (left<right?left:right)+1;
}
}
};
* 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) {
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left&&root->right==NULL)
return minDepth(root->left)+1;
else if(root->right&&root->left==NULL)
return minDepth(root->right)+1;
else if(root->left&&root->right)
{
int left=minDepth(root->left);
int right=minDepth(root->right);
return (left<right?left:right)+1;
}
}
};