首页 > 其他 > 详细

leetcode-Minimum Depth of Binary Tree

时间:2014-10-29 12:08:08      阅读:150      评论:0      收藏:0      [点我收藏+]

 

 1 class Solution {
 2 public:
 3     int minDepth(TreeNode *root) {
 4         if (root == nullptr) return 0;
 5         if (root->left != nullptr&&root->right != nullptr)
 6         {
 7             int lDepth = minDepth(root->left);
 8             int rDepth = minDepth(root->right);
 9             return (lDepth<rDepth?lDepth+1:rDepth+1);
10         }
11         if (root->left != nullptr)
12         {
13             return 1+minDepth(root->left);
14         }
15         if (root->right != nullptr)
16         {
17             return 1+minDepth(root->right);
18         }
19         return 1;//只有一个根节点时。
20     }
21 };

 

leetcode-Minimum Depth of Binary Tree

原文:http://www.cnblogs.com/forcheryl/p/4058889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!