/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root == NULL)
{
return 0;
}
else if (root->left != NULL && root->right == NULL)
{
return 1 + minDepth(root->left);
}
else if (root->left == NULL && root->right != NULL)
{
return 1 + minDepth(root->right);
}
else
{
return 1 + min(minDepth(root->left), minDepth(root->right));
}
}
};
LeetCode-Minimum Depth of Binary Tree
最新推荐文章于 2024-11-14 20:04:48 发布