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 the largest value in each row of a binary tree.
Example:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
BFS Accepted
相比之前的几道用到BFS算法的题目,这题相对来说会繁琐一些,需要用变量记录每一个节点所在的层数l,当前比较大小的层数m,重要的是厘清思路,其实也挺简单的,就是容易脑子搭线,陷入僵局。
/**
* 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 {};
queue<TreeNode*> q;
queue<int> level;
vector<int> ans;
q.push(root);
level.push(0);
int m = -1;
while(!q.empty()) {
TreeNode* temp = q.front();
q.pop();
int l = level.front();
level.pop();
if (temp->left) {
q.push(temp->left);
level.push(l+1);
}
if (temp->right) {
q.push(temp->right);
level.push(l+1);
}
if (l > m) {
m = l;
ans.push_back(temp->val);
} else {
ans[l] = max(ans[l], temp->val);
}
}
return ans;
}
};
LN : leetcode 515 Find Largest Value in Each Tree Row的更多相关文章
- (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 ...
- 【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) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
随机推荐
- JVM定位程序假死或cpu占用高的线程
linux系统: 参考:https://blog.csdn.net/qq_40197576/article/details/80287515 1>使用top命令查看占用cpu进程情况,得到jav ...
- Systemtap工具查看系统资源使用
http://www.xbwolf.com/507 http://blog.csdn.net/kafeiflynn/article/details/6429976 安装步骤yum install sy ...
- sql 按中文排序
sql server:select * from [表名]order by [字段],[字段] collate Chinese_PRC_CS_AS_KS_WS mysql:select * from ...
- Oracle将一列值逗号拼接wm_concat函数
--Oracle12c不再支持该函数,需要手动处理 --管理员登录授权给用户权限 GRANT CREATE SYNONYM TO c##sdic; / --创建 TYPE CREATE OR REPL ...
- Servlet学习笔记(八)—— 文件下载
一.文件下载概述 比如图片或者HTML这类静态资源,仅仅要在浏览器中打开正确的网址就行下载.仅仅要资源放在应用程序文件夹或者其下的子文件夹中,但不在WEB-INF下.Servlet/JSP容器就会将资 ...
- hdu 5386 Cover (暴力)
hdu 5386 Cover Description You have an matrix.Every grid has a color.Now there are two types of oper ...
- Android MaoZhuaWeiBo开发Service抓取个人信息-2
前面把基本的东西讲完了,之后就是数据的获取和解析显示出来了,那接下来我们就负责抓取数据的这块吧,首先我们须要 在清单文件中载入服务和活动 加入:. <activity android:name= ...
- NSArray和NSMutableArray的常用方法 (转)
NSArray和NSMutableArray的常用方法 (转) (2013-09-06 15:13:46) 标签: it 分类: ios编程 初始化方法: 1.init返回一个空数组 2.i ...
- C#下JSON字符串的反序列化
C#下JSON字符串的反序列化,一般都是用newtonsoft.json,比较方便..net当然也有提供相应功能,但觉得比较复杂. 所谓反序列化,就是将一个包含JSON内容的字符串,转换回指定对象(不 ...
- 安装linux系统-CentOS-6.8-x86_64-minimal.iso
1: 2: 3:单击[Next]继续安装. 4:安装语言,选择[Chinese(Simplified)(中文(简体))]菜单,单击[Next]继续. 5:系统键盘,选择[美国英语式]菜单,单击[下一步 ...