题目描述

题目描述
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. 【Sqoop学习之一】Sqoop简介

    环境 sqoop-1.4.6 Sqoop:将关系数据库(oracle.mysql.postgresql等)数据与hadoop数据进行转换的工具. 两个版本:两个版本完全不兼容,sqoop1使用最多:s ...

  2. 高级UI-Path和PathMeasure

    Path是一个工具类,用来记录线条的轨迹路径,然后通过绘制轨迹路径,可以得到各种各样的图案,而PathMeasure是用来对Path进行测量的工具,再Path的运用中,运用最多的就是贝塞尔曲线,也是本 ...

  3. MySQL执行计划值type,强烈推荐

    表结构: create table user ( id int primary key, name varchar(), sex varchar(), index(name) )engine=inno ...

  4. HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)

    Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...

  5. SpringBoot整合AbstractRoutingDataSource实现读写分离

    在配置数据源时候,已经把主库和从库的数据源配置到DynamicDataSource里了 利用AbstractRoutingDataSource实现动态切换数据源,可以通过注解或者根据方法名前缀切换要使 ...

  6. 11 Reponse对象+ServletContext对象

    1.HTTP协议: (1)请求消息:客户端发送给服务器端的数据 数据格式: 1. 请求行 2. 请求头 3. 请求空行 4. 请求体 (2)响应消息:服务器端发送给客户端的数据 * 数据格式: 1. ...

  7. 2.RabbitMQ 的可靠性消息的发送

      本篇包含 1. RabbitMQ 的可靠性消息的发送 2. RabbitMQ 集群的原理与高可用架构的搭建 3. RabbitMQ 的实践经验   上篇包含 1.MQ 的本质,MQ 的作用 2.R ...

  8. python使用matplotlib在一个图形中绘制多个子图以及一个子图中绘制多条动态折线问题

    在讲解绘制多个子图之前先简单了解一下使用matplotlib绘制一个图,导入绘图所需库matplotlib并创建一个等间隔的列表x,将[0,2*pi]等分为50等份,绘制函数sin(x).当没有给定x ...

  9. day41——数值类型、完整性约束

    day41 数值类型 整数类型 有符号的设置 mysql> create table t1(id tinyint); # 默认有符号,即数字前有正负号 无符号的设置 mysql> crea ...

  10. Redis学习笔记(一)— 基本命令和数据类型

    MacOs环境 Redis基本命令 启动服务:redis-server 连接服务:redis-cli -h 指定主机/IP -p 指定端口 -a 指定密码 关闭服务:先shutdown 保存数据并关闭 ...