您的位置:首页 > 其它

LeetCode Minimum Depth of Binary Tree

2014-03-11 19:08 330 查看
没什么难度的,刚开始又马虎了。。。

int minDepth(TreeNode *root) {
if(root==NULL)
return 0;
if (root->left==NULL&&root->right!=NULL)
{
return minDepth(root->right)+1;
}
else
{
if (root->left!=NULL&&root->right==NULL)
{
return minDepth(root->left)+1;
}
else
{
if (root->left!=NULL&&root->right!=NULL)
{
//return minDepth(root->right)+1;
int leftMinDepth = minDepth(root->left);
int rightMinDepth = minDepth(root->right);
return leftMinDepth<rightMinDepth?leftMinDepth+1:rightMinDepth+1;
}
else
{
return 1;
}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: