[牛客网 -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 ...
随机推荐
- Java之第一个Java程序
编写第一个Java程序 打开文本编辑器,输入以下代码 public class Hello { public static void main(String[] args) { System.out. ...
- 进程间之异步通信:信号Signal
信号 信号是进程间通信机制中唯一的异步通信机制:信号机制是进程间传递消息的一种机制,是异步进程中通信的一种方式 一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号 内核处理一个进程收到的软中断 ...
- 2、Maven的简介和配置
1.下载Maven apche-maven-3.5.2 2. 三.maven简介 1.基于Ant的构建工具,Ant有的功能Maven都有,额外添加了其他的功能 2.运行原理图 2.1本地仓库:计算机 ...
- Vue(六)插槽(2.6.0+)
插槽在vue2.6.0开始有了新的更新 具名插槽(数据来自父组件) 子组件(定义插槽)这里版本前后没什么变化 <template> <div> <header> & ...
- SpringBoot整合AbstractRoutingDataSource实现读写分离
在配置数据源时候,已经把主库和从库的数据源配置到DynamicDataSource里了 利用AbstractRoutingDataSource实现动态切换数据源,可以通过注解或者根据方法名前缀切换要使 ...
- Java反射桥接方法解析
在阅读mybaits源码的反射模块时,看到了如下的一段代码: /** * 添加 Method 数组到 uniqueMethods * @param uniqueMethods * @param met ...
- SpringBoot(1)
SpringBoot 8/2 CRUD 发送put请求修改数据有三个步骤: SpringMVC中配置HiddenHttpMethodFilter 页面上创建一个post请求(form标签只能写get和 ...
- 从Harbor仓库拉起镜像,创建容器并更新shell脚本
注意: 此shell脚本仅供基本使用,还有好多待完善的地方 大致流程 使用Jenkins从Gogs拉取仓库代码,根据选择的参数和输入的标签,确定要编译打包jar的模块,以及要制作的docker镜像信息 ...
- caffe基础入门
学的太浅,先放一个别人写的吧,等自己摸清楚回来搞搞. https://blog.csdn.net/cham_3/article/details/72141753
- REDISTEMPLATE如何注入到VALUEOPERATIONS
REDISTEMPLATE如何注入到VALUEOPERATIONS 今天看到Spring操作redis 是可以将redisTemplate注入到ValueOperations,避免了ValueOpe ...