[牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度
题目描述
题目描述
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 -树的最短深度的更多相关文章
- [牛客网 -leetcode在线编程 -01] max-points-on-a-line -穷举
题目及题目来源 链接:https://www.nowcoder.com/questionTerminal/bfc691e0100441cdb8ec153f32540be2 来源:牛客网 首页 > ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
随机推荐
- python:单例模式--使用__new__(cls)实现
单例模式:即一个类有且仅有一个实例. 那么通过python怎么实现一个类只能有一个实例呢. class Earth: """ 假如你是神,你可以创造地球 "&q ...
- 存储Flash--NOR flash和 Nand flash
flash是存储芯片的一种,通过特定的程序可以修改里面的数据.FLASH在电子以及半导体领域内往往表示Flash Memory的意思,即平时所说的“闪存”,全名叫Flash EEPROM Memory ...
- 给通过canvas生成的二维码添加logo
以jquery.qrcode为例来说, 生成二维码代码: 依赖jquery.js, jquery.qrcode.js //计算宽,高,中心坐标,logo大小 var width = 256,heigh ...
- layui父页面执行子页面方法
parent.window[layero.find('iframe')[0]['name']].子页面方法(); layero.find('iframe')[0].contentWindow.子页面方 ...
- SpringBoot + Mybaties的逆向工程有数据库生成domain的过程
环境: jdk1.8 (适合springboot2.X以上版本) Maven(3.3.X以上) spring boot 2.1.6 Idea 2019.1\ 这里随便填 选择相应的Jar,如以下的勾 ...
- 使用JMeter进行Apache Kafka负载测试
1.卡夫卡负载测试 在这个Apache Kafka教程中,我们将了解如何使用Apache JMeter,如何在Apache Kafka上执行Kafka负载测试.此外,这个Kafka负载测试教程教我们如 ...
- Apache Kafka Producer For Beginners
在我们上一篇Kafka教程中,我们讨论了Kafka Cluster.今天,我们将通过示例讨论Kafka Producer.此外,我们将看到KafkaProducer API和Producer API. ...
- 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...
- Java开发笔记(一百二十七)Swing的标签
提起AWT的标签控件Label,那个使用体验可真叫糟糕,不但不支持文字换行,而且对中文很不友好,既可能把中文显示为乱码,还不支持博大精深的各种中文字体.所幸Swing的升级版标签JLabel在各方面都 ...
- 安装Delphi7的错误
delphi7运行不正常的提示unable to rename'c:\program files\Borland\delphi7\Bin\delphi32.$$$'to'c:\program file ...