思路:这道题的话比较容易想通的,就利用队列进行层次遍历,每下一层就将高度+1,然后在每一层的遍历中,如果发现某个结点没有孩子结点,那么它就是高度最小的叶子结点, 由此得到这棵树的最小深度;

struct TreeNode
{int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};int minDepth(TreeNode *root)
{if (root == NULL)  //该树为空 直接返回0return 0;queue<TreeNode *> q;q.push(root);   //把根结点放入队列int result = 0;while (!q.empty()){queue<TreeNode *> qq;  //辅助层次队列result++;while (!q.empty()){TreeNode *now = q.front();q.pop();if (now->left == NULL && now->right == NULL) //如果当前结点没有孩子结点,直接就作为深度最小的叶子节点,返回深度值return result;if (now->left)//如果不是的话,就把它非空的孩子结点放入队列中{qq.push(now->left);}if (now->right){qq.push(now->right);}}q = qq;}return result;
}

Leetcode每日一题:111.minimum-depth-of-binary-tree(二叉树的最小深度)相关推荐

  1. 111. Minimum Depth of Binary Tree 二叉树的最小深度

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

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

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

  3. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

  4. 111. Minimum Depth of Binary Tree

    1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...

  5. LeetCode: 111. Minimum Depth of Binary Tree

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

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

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

  8. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  9. LeetCode每日一题(Path In Zigzag Labelled Binary Tree)

    In an infinite binary tree where every node has two children, the nodes are labelled in row order. I ...

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

最新文章

  1. mkdir和mkdir-p的区别
  2. 小程序 text decode 真机无效_【移动端测试】APP自动化测试案例2:微信小程序自动化测试...
  3. asp.net ajax技巧6
  4. Scala in depth 6 Scala的类型系统 中
  5. Blazor确认复选框组件
  6. Linux - history命令详解
  7. 转帖node详细教程
  8. 【老生谈算法】matlab实现自适应Simpson积分算法源码——积分算法
  9. [转贴]杨式太极拳八十五式通释—2—王志远
  10. Openssh rpm 包制作
  11. 小虎队《爱》 —— 米扑科技公司司歌
  12. 软件测试1——PIE模型
  13. 高配云计算机平台,云电脑真的来了!小破本也能秒变高配
  14. SQL Server 2005 连接到服务器失败解决方案
  15. Android开发之
  16. 礼多人不怪:跟美国教授通邮件的18种礼仪
  17. java 一个线程占多少内存_一个jvm线程占用多少操作系统内存
  18. 文件搜索与文本内容查看
  19. Android OTA 升级之五:updater
  20. 度小满9.20测开笔试第二题——相似字符串

热门文章

  1. Go语言基础练习题系列1
  2. Spring 多线程
  3. C++设计模式--观察员
  4. thinkphp的like用法
  5. 关于NavigateUrl中绑定Eval()方法时出现服务器标记的格式不正确的解决方法
  6. NumPy基础操作(1)
  7. 5、SQL Server数据库、T-SQL
  8. 使用crypt生成用户密码-Tested on RedHat SuSE Platform
  9. PhpMyAdmin 配置文件现在需要一个短语密码的解决方法
  10. 记录sqoop同步失败问题解决过程,过程真的是很崎岖。(1月6日解决)