515 Find Largest Value in Each Tree Row 在每个树行中找最大值
在二叉树的每一行中找到最大的值。
示例:
输入:
1
/ \
3 2
/ \ \
5 3 9
输出: [1, 3, 9]
详见:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/
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) {
if(!root)
{
return {};
}
vector<int> res;
queue<TreeNode*> que;
que.push(root);
int cur=1;
int mx=INT_MIN;
while(!que.empty())
{
root=que.front();
que.pop();
--cur;
if(root->val>mx)
{
mx=root->val;
}
if(root->left)
{
que.push(root->left);
}
if(root->right)
{
que.push(root->right);
}
if(cur==0)
{
cur=que.size();
res.push_back(mx);
mx=INT_MIN;
}
}
return res;
}
};
515 Find Largest Value in Each Tree Row 在每个树行中找最大值的更多相关文章
- Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 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 ...
- Java实现 LeetCode 515 在每个树行中找最大值
515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...
- [Swift]LeetCode515. 在每个树行中找最大值 | 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. 在每个树行中找最大值
题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...
- 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 ...
- (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 ...
- 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 / \ ...
随机推荐
- aop中获取方法的注解
@Around(value="@annotation(apiLog)") public Object around(ProceedingJoinPoint pjp, ApiLog ...
- HTML canvas
什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...
- 自制简单的range(Vue)
废话不多说先上成果图 实现思路 主要分界面与逻辑两大块 界面分为5个部分 左滑块长度 左内容位置 中间长度 右滑块长度 右内容位置 逻辑 touch3个事件 各滑块长度及位置计算 选中时变色 具体实现 ...
- HDU2089 不要62 —— 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) M ...
- Spring Boot2.0之整合Redis
需要的maven依赖 jar包,是对Jedis的封装 maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)
NSString* str=@"hello";//存在代码区,不可变 NSLog(@"%@",str); //1.[字符串插入] NSMutableString ...
- silverlight 使用IValueConverter 转换
在绑定数据中 有时候我们需要转换相关数据类型 silverlight提供了一个System.Windows.Data.IValueConverter接口它提供两个方法 Convert和ConvertB ...
- Identifier expected after this token
Cursor cursor = db.query(true, "user", new String[]{"id","mode"}, &quo ...
- 【伪题解】 [Offer收割]编程练习赛58
[A:最大的K-偏差排列]: 第一次在hiho卡一题,所以暴力了搜索了一下,70分,后面回来打表找规律,规律是有和K有关的周期. 当K<=N/2时,成周期交叉变化,最后尾部部分单独考虑. 当K& ...
- Transformations
链接 分析:根据操作模拟 /* ID:wanghan PROB:transform LANG:C++ */ #include "iostream" #include "c ...