题目描述

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

代码及思路注释

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
/*
大致题意: 求二叉树的最短树的深度,从root结点到叶子结点的最短路
*/
struct TreeNode{
TreeNode * left;
TreeNode * right;
};
class Solution {
public:
int run(TreeNode *root) {
//1.空树
if(!root){
return 0;
}
queue<TreeNode *> Q; //进行层次(广度)遍历
Q.push(root);
int level=1;
int now=1;
int cnt=1; while(Q.size()>0){
cnt=now;
now=0; //如下遍历cnt个节点, 将cnt个结点全部遍历查找
while(cnt--){
//取出queue第一个结点
TreeNode * t=Q.front();
Q.pop(); if(!t->left&&!t->right)
return level; if(t->left){
Q.push(t->left);
now++;
}
if(t->right){
now++;
Q.push(t->right);
} } level++;
} return 9999;
}
}; int main(int argc, char** argv) { return 0;
} /*
ctrl+E: 复制当前行
ctrl+D: 删除当前行 */

[牛客网 -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. Graphviz(02) 汉字subgraph不显示汉字的处理

    1.按照官方教程设置 2.如果还不显示,在不显示的汉字前面加上一个“空格”,汉字就会正常显示. digraph idp_modules{ rankdir = TB; fontname = " ...

  2. 第07组 Beta冲刺(1/4)

    队名:秃头小队 组长博客 作业博客 组长徐俊杰 过去两天完成的任务:学习了很多东西 Github签入记录 接下来的计划:继续学习 还剩下哪些任务:后端部分 燃尽图 遇到的困难:自己太菜了 收获和疑问: ...

  3. pip修改成国内镜像源

    临时指定镜像源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple seaborn 永久修改镜像源 linux下,修改 ~/.pip/pip ...

  4. String初解

    String 类型是不可变的对象,因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后有一次看到一个代码段,如下: 这段代码返回的是ture,当时以为是f ...

  5. Kafka排队:Apache Kafka作为消息传递系统

    1.目标 在这个Apache Kafka教程中,我们将学习Apache Kafka  Queuing 的概念  .基本上,Kafka中的排队是传统消息传递的模型之一.所以,让我们首先简要介绍Kafka ...

  6. SpringMVC笔记1

    SpringMVC是一个一种基于Java的实现MVC设计模型的请求驱动类型的轻量级web框架 SpringMVC的入门案例 2.导入相关jar包 <?xml version="1.0& ...

  7. 模块 logging random

    模块logging logging模块的主要功能是记录软件调试.操作过程中的各种日志. 默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志, ...

  8. golang ---获取磁盘信息

    package main import ( "fmt" "github.com/StackExchange/wmi" ) type Storage struct ...

  9. [转]mongodb authentication 设置权限之后,新建个管理账户和一般数据库用户,在win 7 64bit 环境下测试使用实例

    如果之前安装mongodb时没有使用 --auth,那么必须要卸载MongoDB服务,进行重新安装,设置账号权限才生效! 主要是解决在测试使用mongo db 时候,总是出现的MongoAuthent ...

  10. MySQL-5.7.26解压版安装教程

    1.下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.26-winx64.zip 2.在解压目录中添加 data文件夹 和 my.in ...