[牛客网 -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 ...
随机推荐
- [LeetCode] 685. Redundant Connection II 冗余的连接之 II
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- 【视频开发】非常优秀的CxImage各种格式图像开源工具
下面介绍应用它进行图像类型转换的方式: 1.从一种图像文件类型转换为另一种文件类型(convert from a format to another) CxImage image; // ...
- SpringMVC中静态资源的处理
web项目中web.xml配置 在一个使用springmvc的web项目中,必然在web.xml中要配置前端控制器DispatcherServlet <servlet> <servl ...
- 【转】Fuel-Openstack的搭建(一)
原文链接:https://blog.csdn.net/qq_35180983/article/details/82181496 2.1安装前的准备操作: 首先,我们需要准备以下操作: 2.1.1 下载 ...
- python_scratch教程:绘制荧光圈教程
在演唱会中,出现最多的就是荧光棒,很多粉丝也都会举着荧光棒为自己的爱豆加油,当然也会有一些人用的荧光圈,为此南京小码王scratch培训机构,就想到了和小朋友一起绘制荧光圈,现在我们就一起来看看吧! ...
- Go语言( 运算符)
运算符用于在程序运行时执行数学或逻辑运算. 运算符 Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 算数运算符 运算符 描述 + 相加 - 相减 * 相乘 / 相 ...
- 【C#】上机实验二
实验1: 求解 1/1 + 1 / 2 + 1 / 3 + 1 / 4 …… + 1 / i = ? 确保精度在 1e-6内. using System; using System.Collect ...
- PB连接ORALCE数据库方法
本地必须配置连接服务
- spring boot使用@Aspect记录日志(请求参数,响应结果)
- docker查看容器日志
原文:docker查看容器日志 前言 $ sudo docker logs -f -t --tail 行数 容器名 1 2 1.命令查看 root@c68d4b5dd583c4f4ea30da2989 ...