您的位置:首页 > 其它

leetCode 之 Maximum Depth of Binary Tree

2015-06-09 11:01 405 查看
LeetCode : Maximum Depth of Binary Tree

题目原意:求二叉树的最大深度

注意:采用递归,要特别注意返回条件

代码如下(leetCode 测得运行时间为4ms):

int maxDepth(struct TreeNode *root)
{
int depth_right = 1;
int depth_left  = 1;
if (!root)
{
return 0;
}

while(root)  //!< 采用递归,注意返回条件
{
if (root->left)
{
depth_left = depth_left + maxDepth(root->left);
}

if (root->right)
{
depth_right = depth_right + maxDepth(root->right);
}
return depth_left >= depth_right ? depth_left  : depth_right ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: