您的位置:首页 > 其它

Minimum Depth of Binary Tree

2016-06-21 13:50 246 查看
二叉树的最小深度

int minDepth(TreeNode *root)
{
return minDepth(root, false);
}
int minDepth(TreeNode *root, bool hasbrothers)
{
if (root == nullptr)return hasbrothers ? INT_MAX : 0;

return 1 + min(minDepth(root->left, root->right != nullptr),
minDepth(root->right, root->left != nullptr));

}


View Code
同理可判断最大深度,因为是求最大值,所以无需判断该结点是否是叶子结点(如果不是叶子结点,肯定不是最大深度)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: