[牛客网 -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 ...
随机推荐
- PHPExcel 中文使用手册详解 二
$objPHPExcel = new \PHPExcel(); //定义配置 $topNumber = 2;//表头有几行占用 $xlsTitle = iconv('utf-8', 'gb2312', ...
- phpexcel无法导出的解决方法
phpexcel无法导出的解决方法 <pre> set_time_limit(0); ini_set("memory_limit","512M"); ...
- canal部署启动的时候报错com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set
报错信息:com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set 解决方法: 找 ...
- 看看这5个最容易犯的Java错误,你犯了没?
人非圣贤,孰能无过.都说Java语言是一门简单的编程语言,基于C++演化而来,剔除了很多C++中的复杂特性,但这并不能保证Java程序员不会犯错.那么对于广大的Java程序员来说,它们最容易犯的几个错 ...
- PMBOK(第六版) PMP备考知识总汇!
记录本人学习PMBOK第六版的学习笔记. 备考知识总汇! PMBOK序章 PMP备考指南之相关事项介绍 PMP备考指南之第一章:引论 PMP备考指南之第二章:项目运作环境 PMP备考指南之第三章:项目 ...
- 使用flask做网页的excel成绩分析
使用到的技术:pyecharts flask 首先 pip install flask 和下载pip install pyecharts==0.5.5 项目结构: 代码: from flask imp ...
- SQL——LIKE操作符
一.LIKE操作符的基本用法 LIKE操作符用于在WHERE子句中,搜索相似.类似的数据. LIKE操作符语法: SELECT 列名1,列名2... FROM 表名 WHERE 列名 LIKE xxx ...
- 17.Python略有小成(包,logging模块)
Python(包,logging模块) 一.包 什么是包 官网解释 : 包是一种通过使用'.模块名'来组织python模块名称空间的方式 , 具体来讲 , 包就是一个包含有__ init __.py文 ...
- LeetCode第8场双周赛(Java)
这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...
- GIT讲解
一.什么是Git: Git是目前世界上最先进的分布式版本控制系统. 二.为什么要用版本控制系统: 1.更方便的存储版本 2.恢复之前的版本 3.更方便的进行对比 4.协同合作 三.如何安装GIT: 1 ...