题目描述

  1. 题目描述
  2. 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.

代码及思路注释

  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. using namespace std;
  4. /*
  5. 大致题意: 求二叉树的最短树的深度,从root结点到叶子结点的最短路
  6. */
  7. struct TreeNode{
  8. TreeNode * left;
  9. TreeNode * right;
  10. };
  11. class Solution {
  12. public:
  13. int run(TreeNode *root) {
  14. //1.空树
  15. if(!root){
  16. return 0;
  17. }
  18. queue<TreeNode *> Q;
  19. //进行层次(广度)遍历
  20. Q.push(root);
  21. int level=1;
  22. int now=1;
  23. int cnt=1;
  24. while(Q.size()>0){
  25. cnt=now;
  26. now=0;
  27. //如下遍历cnt个节点, 将cnt个结点全部遍历查找
  28. while(cnt--){
  29. //取出queue第一个结点
  30. TreeNode * t=Q.front();
  31. Q.pop();
  32. if(!t->left&&!t->right)
  33. return level;
  34. if(t->left){
  35. Q.push(t->left);
  36. now++;
  37. }
  38. if(t->right){
  39. now++;
  40. Q.push(t->right);
  41. }
  42. }
  43. level++;
  44. }
  45. return 9999;
  46. }
  47. };
  48. int main(int argc, char** argv) {
  49. return 0;
  50. }
  51. /*
  52. ctrl+E: 复制当前行
  53. ctrl+D: 删除当前行
  54. */

[牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度的更多相关文章

  1. [牛客网 -leetcode在线编程 -01] max-points-on-a-line -穷举

    题目及题目来源 链接:https://www.nowcoder.com/questionTerminal/bfc691e0100441cdb8ec153f32540be2 来源:牛客网 首页 > ...

  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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  4. [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 ...

  5. [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 ...

  6. [Leetcode] The 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. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

  9. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

随机推荐

  1. 阿里云移动推送 ios项目添加SDK步骤

    添加阿里云Pods仓库和各产品SDK Pod依赖,配置步骤如下: 1. CocoaPods集成添加阿里云Pods仓库,Podfile添加: source 'https://github.com/ali ...

  2. springboot实战日记(一)数据库基本信息

    摘要:基于spring boot的后端实现,开发一个微信小程序点餐系统,主要是写写思路和遇到的问题以及分享读到的好文章. 项目分析: 1.角色划分,就是开有什么人使用这个系统,买家(手机端),卖家(p ...

  3. Python 获取文件类型后缀

    import os path='file.txt' file=os.path.splitext(path) filename,type=file print(filename) print(type)

  4. 使用pyinstaller将python打包成exe文件

    步骤: 1)win+R 输入cmd打开dos窗口   2)先安装pyinstaller: pip install pyinstaller 3)然后使用cd命令进入项目文件的路径下:   4)再使用命令 ...

  5. php_mvc实现步骤三,四

    3.match_mvc MVC 以ecshop的前台为例: 功能一: 首页 购物车数据,商品分类数据,其他的首页需要的数据 功能二: 拍卖活动 购物车数据,商品分类数据,拍卖相关数据 功能三: 团购商 ...

  6. el-input 标签中密码的显示和隐藏

    效果展示: 密码隐藏: 密码显示: 代码展示: 一:<el-input>标签代码 <el-form-item label="密码" prop="pass ...

  7. Java开发笔记(一百二十五)AWT图像加工

    前面介绍了如何使用画笔工具Graphics绘制各种图案,然而Graphics并不完美,它的遗憾之处包括但不限于:1.不能设置背景颜色:2.虽然提供了平移功能,却未提供旋转功能与缩放功能:3.只能在控件 ...

  8. 爬虫请求库之requests库

    一.介绍 介绍:使用requests可以模拟浏览器的请求,比之前的urllib库使用更加方便 注意:requests库发送请求将网页内容下载下来之后,并不会执行js代码,这需要我们自己分析目标站点然后 ...

  9. REST-framework之频率组件

    REST-framework之频率控制 一 频率简介 为了控制用户对某个url请求的频率,比如,一分钟以内,只能访问三次 二 自定义频率类,自定义频率规则 自定义的逻辑 ""&qu ...

  10. prometheus grafana graylog 钉钉告警 短信告警 电话告警系统 PrometheusAlert

    PrometheusAlert 简介 PrometheusAlert是开源的运维告警中心消息转发系统,支持主流的监控系统Prometheus,日志系统Graylog和数据可视化系统Grafana发出的 ...