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 ;
}
}