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.
给定一棵二叉树,找到它的最小深度。最小深度是沿着从根节点到最近叶节点的最短路径的节点数量。
class Solution {
public:
typedef TreeNode* tree;
int run(TreeNode *root) {
if(!root)
return ;
queue<tree> q;
q.push(root);
tree cur,last;
int level=;
last=q.back();
while(q.size()){
cur=q.front();
q.pop();
if(cur->left==NULL&&cur->right==NULL)
break;
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right);
if(last==cur){
level++;
last=q.back();
}
}
return level;
}
};
minimum-depth-of-binary-tree——二叉树遍历、链表、广度优先的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [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 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- [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] Minimum Depth of Binary Tree 二叉树最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
随机推荐
- django(django框架了解,http协议)
Django框架 学习目的: 完成web应用的编写 django的作用: 0.业务逻辑分发(路由分发) 1.业务逻辑实现: 业务逻辑根据分发来完成具体的事,再根据具体事的需求,和页面或数据库交互,返回 ...
- Java-计算程序运行时间
package com.tj; @SuppressWarnings("unused") public class CountTime { public static void ma ...
- 【02】你是如何理解 HTML 语义化的,有什么好处
[02]你是如何理解 HTML 语义化的 01,语义化,就是通过HTML标签来表示页面包含的信息. 02,其中有HTML标签的语义化和CSS命名的语义化. 03,HTML标签语义化的的含义是: ...
- Wp8 读取手机信息
/// <summary> /// 获取系统信息 /// </summary> private void GetSystemInfo() { lblMsg.Text = str ...
- 大数据学习——Hbase
1. Hbase基础 1.1 hbase数据库介绍 1.简介 hbase是bigtable的开源java版本.是建立在hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写nosql的数据库系统 ...
- linux下连接到远程主机,用图像界面(想在远程服务器上用cmake)
1. 需要通过SSH -X username@ip登陆服务器后,再用图形界面,比如用cmake 2.直接用 SSH username@ip命令登陆服务器后,不能用cmake
- iOS学习笔记20-地图(二)MapKit框架
一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...
- BZOJ2654 tree 【二分 + 最小生成树】
题目 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 输入格式 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行, ...
- 解决 sqlalchemy 报错:(1193, "Unknown system variable 'tx_isolation'")
1出现此报错的原因是使用的mysql8.0 以前用的是:tx_isolation 现在用是: transaction_isolation a.通过升级 sqlalchemy 的方法可以解决此问题, p ...
- Redis的持久化——AOF
上一篇博文给大家介绍了redis持久化的方式之一RDB,其中说到过RDB的缺陷是可能会导致数据丢失严重,所以redis的作者 由于强迫症又开发出了AOF来你补这一不足.好接下来我将为大家介绍AOF. ...