111. Minimum Depth of Binary Tree
1.条件不能是root == NULL,而是判断叶子结点的条件。
class MinDepth {
public:
int minDepth(TreeNode* root) {
if (root == NULL)
{
return 0;
}
return minDepthCore(root);
}
int minDepthCore(TreeNode* root)
{
if (root->left == NULL && root->right == NULL)
{
return 1;
}
int left = INT_MAX;
if (root->left != NULL)
{
left = minDepthCore(root->left);
}
int right = INT_MAX;
if (root->right != NULL)
{
right = minDepthCore(root->right);
}
return left < right ? left + 1 : right + 1;
}
};