题目:

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

代码:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int minDepth(TreeNode* root) {if (!root) return 0;if ( !root->left && !root->right) return 1;if ( root->left && root->right ) return std::min( Solution::minDepth(root->left)+1, Solution::minDepth(root->right)+1);if ( root->left ) return Solution::minDepth(root->left)+1;if (root->right ) return Solution::minDepth(root->right)+1;}
};

tips:

深搜思路(递归实现)。这里需要控制向下进行的条件。

1. 如果root是NULL,返回0

2. 如果root不是NULL,且left和right都是NULL,则到达叶子节点返回1(代表算上叶子节点的那一层)

3. 如果root->left和root->right都不为NULL,则继续往两边深搜

4. 如果root不是NULL,但root->left或root->right哪一方为NULL,则为NULL的一端不会再有叶子节点出现,不能再往下走了。

完毕。

============================================

第二次过这道题,上来没有把终止条件想完全。终止条件应该是达到叶子简单root->left root->right都为空。

修改了一次后AC了。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int minDepth(TreeNode* root){if ( !root ) return 0;int min_depth = INT_MAX;Solution::depth(root, min_depth, 1);return min_depth;}static void depth(TreeNode* root, int& min_depth, int dep){if ( !root->left && !root->right ) min_depth = min(min_depth,dep);            if ( root->left ) Solution::depth(root->left, min_depth, dep+1);if ( root->right ) Solution::depth(root->right, min_depth, dep+1);}
};

转载于:https://www.cnblogs.com/xbf9xbf/p/4508762.html

【Minimum Depth of Binary Tree】cpp相关推荐

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  3. leetcode - Minimum Depth of Binary Tree

    题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...

  4. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  5. leetcode:Minimum Depth of Binary Tree【Python版】

    1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...

  6. LeetCode Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. LeetCode 111. Minimum Depth of Binary Tree

    原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  8. [LeetCode] Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. C#LeetCode刷题之#111-二叉树的最小深度​​​​​​​(Minimum Depth of Binary Tree)

    问题 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 给定二叉树 [3,9,20,null,null,15,7], ...

最新文章

  1. boost::fusion::reverse_view用法的测试程序
  2. 音视频技术开发周刊 | 140
  3. 利用redis List队列简单实现秒杀 PHP代码实现
  4. 指标波动多大才算是异常?
  5. 从技术分工的角度来看996.ICU
  6. SpringBoot+Shiro+ehcache实现登录失败超次数锁定帐号
  7. 绩效管理这样做,成本减半,员工叫好!
  8. csgo自动选择服务器,CSGO服务器怎么选择合适的配置?CSGO服务器如何选择系统?...
  9. 传智播客Java 关键字,标识符,注释
  10. 基于SURF特征的图像与视频拼接技术的研究和实现(一)
  11. 用Word2003助你轻松阅读文档(转)
  12. 量化—神话、黑箱与真谛
  13. 身份证归属地查询免费api接口代码
  14. 软件测试工程师如何保证软件的质量?
  15. 2021最新 深圳互联网公司排名
  16. Python netCDF4
  17. 3D打印将对零售模式产生颠覆影响,能否抓住机遇
  18. 最新软件测试面试题,常见面试题及答案汇总,不怕拿不到offer
  19. python如何分割年月日_将日期拆分为年、月和日,分隔符不一致
  20. prometheus监控HBase

热门文章

  1. ue4相机_纳格数字创意课程介绍 |UE4虚拟现实技术室内方向
  2. Linux共享文件夹中毒,Linux find命名快速查找中毒文件操作实例
  3. 算法篇之-----滑动窗口(尺取法)
  4. 华为云创建免费服务器的一次失败尝试
  5. 2021年信息系统项目管理师案例分析第二题讲解
  6. 整理了十个Python自动化操作
  7. Vue中使用vue-video-player和videojs-flash插件实现播放rtmp视频文件流
  8. FastReport安装包下载、安装、去除使用限制以及工具箱中添加控件
  9. 从实例入手学习Shiro与Web的整合
  10. BJUI修改弹窗dialog的宽度和高度