leetCode 之 Maximum Depth of Binary Tree

来源:互联网 发布:mac自动播放照片 编辑:程序博客网 时间:2024/05/17 07:05
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 ;}}


0 0