(BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.
Example:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
--------------------------------------------------------------------------
就是找出二叉树的每一层的最大数。用BFS较为简单。
C++代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> vec;
if(!root){
return vec;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int res = -; //int类型的最小数。
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
if(t->val > res){
res = t->val;
}
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
vec.push_back(res);
}
return vec;
}
};
(BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row的更多相关文章
- LN : leetcode 515 Find Largest Value in Each Tree Row
lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 515. Find Largest Value in Each Tree Row查找一行中的最大值
[抄题]: You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...
- 515 Find Largest Value in Each Tree Row 在每个树行中找最大值
在二叉树的每一行中找到最大的值.示例:输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [ ...
- 【leetcode】Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
- 515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
- LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
随机推荐
- 包装类接受string 会自动将数字类型string转换成对应得包装类型
- linux硬件数据
Linux 文件详解 lrwxrwxrwx root root 8月 bin -> usr/bin //二进制目录 存放了许多GNU用户极工具 dr-xr-xr-x. root root 8月 ...
- kubernetes 简单yaml文件运行例子deployment
运行一个deployment: kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2 基本例子: nginx-test.y ...
- luogu P1353 【[USACO08JAN]跑步Running】
USACO!!! 唉!无一例外又是母牛(终于知道美国的牧场养什么了) 今天的主人公是一个叫贝茜的公主病母牛(好洋气) 可是她叫什么和我们理解题好像没有什么关系 通过读题我们可以发现她有三个傲娇的地方 ...
- LOJ6432 [PKUSC2018] 真实排名 【组合数】
题目分析: 做三个指针然后预处理阶乘就行. 题目代码: #include<bits/stdc++.h> using namespace std; ; ; int n,k; struct n ...
- 安卓Android基础—第一天
1.1G-4G的介绍 1G 大哥大 2G 小灵通 采用gsm标准(美国军方标准民用化) 发短信 3G 沃 72M/s 4G lte 100M/s 5G 华为 10G/s 小公司卖茶品大公司卖版权(标准 ...
- [HAOI2007] 修筑绿化带
类型:单调队列 传送门:>Here< 题意:给出一个$M*N$的矩阵,每一个代表这一格土地的肥沃程度.现在要求修建一个$C*D$的矩形花坛,矩形绿化带的面积为$A*B$,要求花坛被包裹在绿 ...
- wstngfw中使用Viscosity连接OpenV-P-N服务器
wstngfw中使用Viscosity连接OpenV-P-N服务器 在本例中,将假设以下设置: 站点 A 站点 B 名称 Beijing Office(北京办公室) 名称 Shenzheng Offi ...
- wstngfw IPsec 站点到站点连接示例
wstngfw IPsec 站点到站点连接示例 在本例中,将假设以下设置: IPsec站点设置 站点 A 站点 B 名称 Beijing Office(北京办公室) 名称 Shenzheng Offi ...
- PHP linux ZendGuardLoader.so: undefined symbol: executor_globals
/usr/xxx/php xxx/xxx.php 报了这个错. 本人出现此问题的原因: php执行程序路径错了. 解决: linux下执行 which php 命令 查看php真实路 ...