题目描述:

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.

解法一:非递归方式,使用层序遍历的方法,借助队列,思想比较简单。

int minDepth(TreeNode* root)
{int res = 0;if(!root)return res;else{res = 1;queue<TreeNode*> q;q.push(root);int layer_size_left = q.size();while(!q.empty()){TreeNode * temp = q.front();q.pop();-- layer_size_left;if(temp->left)q.push(temp->left);if(temp->right)q.push(temp->right);if(!temp->left && !temp->right)return res;if(layer_size_left == 0){++ res;layer_size_left = q.size();}}}return res;}

解法二:递归方式,递归方式看上去更简洁一些,而且更易扩展,比如稍作修改即可解出最大深度等,当然非递归方式使用层序遍历解最大深度也不难。

int minDepth(TreeNode* root)
{if(!root)return 0;int l = minDepth(root->left);int r = minDepth(root->right);if(!l)return 1 + r;if(!r)return 1 + l;return l > r ? 1 + r : 1 + l;
}

两者的运行效率差不太多,leetcode上显示运行时间都是9ms,但是都不是最快的解法,应该还有可以优化的地方!这应该是我接下来努力的方向。

转载于:https://www.cnblogs.com/maizi-1993/p/5892696.html

leetcode 111相关推荐

  1. leetcode 111.删点成林 C++

    leetcode 111.删点成林 C++ 删点成林 题目描述 示例 提示 解题思路 C++代码 注意事项 删点成林 题目描述 给出二叉树的根节点 root,树上每个节点都有一个不同的值. 如果节点值 ...

  2. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  3. 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 shor ...

  4. Leetcode 111.二叉树的最小深度

    Time: 20190901 Type: Easy 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: ...

  5. [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)

    问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...

  6. leetcode 111. 二叉树的最小深度

    题目 思路 递归解法,思路直接看注释吧~ 注意对于最小深度定义,有一个小坑,下面这棵树的结果应该是2,而不是1,为此我专门加了一个判断: 如果根部只有一个孩子,则另一侧深度恒为1.此时,应取有孩子的那 ...

  7. [leetcode]111.二叉树的最小深度

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 1: 输入:root = [3,9,20,null,null,1 ...

  8. LeetCode 111二叉树的最小深度-简单

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 1: 输入:root = [3,9,20,null,null,1 ...

  9. 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 ...

最新文章

  1. 安装php出现 “make: *** [ext/gd/libgd/gd_jpeg.lo] Error ”
  2. 大四中软实习笔记201303011文件
  3. kotlin学习笔记——内联函数
  4. excel超级工具箱_这6个Excel高效办公插件,你都用过吗?
  5. treeset java_Java TreeSet last()方法与示例
  6. oracle 布尔转换java布尔_java 布尔值一种赋值方法
  7. ie提示保护计算机关闭网页,xp系统解决IE为保护计算机关闭网页方法分享
  8. 微服务升级_SpringCloud Alibaba工作笔记0004---认识spring gateway理解新一代网关
  9. openVINO2021.4安装记录
  10. JS实现文字截取(雾)
  11. php 进行http请求,php模拟http请求的两种方式
  12. HashSet原理、TreeSet
  13. 胡寿松自动控制原理第七版勘误-152页
  14. android 色彩搭配,色彩搭配利器:最好用的配色工具App Top5
  15. Java面试题(一) 题目:输入某年某月某日,判断这一天是这一年的第几天
  16. 随手记录: 扩展M.2硬盘,从1T换到2T 硬盘clone ubuntu设置等注意事项
  17. 苹果手机使用技巧汇总,手把手教你如何快速使用苹果手机
  18. 移动硬盘文件丢失要如何找回呢?
  19. 【图像处理】初识计算机视觉
  20. FCN(Fully Convolutional Network)与Unet:谈到语义分割不得不提的两个网络

热门文章

  1. android 自定义View 的详细介绍
  2. postman断言作用及怎么使用
  3. Agent编程平台的实现
  4. React文档(一)安装
  5. LeetCode 154 在有序旋转数组中找最小-2
  6. JS的Touch事件们
  7. [BEC][hujiang] Lesson02 Unit1:Working life ---Reading
  8. GDI+中发生一般性错误 以及发布时候需要配置的文件
  9. 我的Dll(动态链接库)学习笔记(转)
  10. 《Android/OPhone开发完全讲义》连载(7):使用SharedPreferences存取复杂数据