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 ...
随机推荐
- maven的安装与环境变量配置
1.下载maven 地址:http://maven.apache.org/download.cgi 点击下载 apache-maven-3.2.1-bin.zip. 2.安装配置,假设maven 解压 ...
- codevs 3164 质因数分解
3164 质因数分解 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description (多数据)给出t个数,求出它的质因子个 ...
- MyBatis 3在Insert之后返回主键
XML: <insert id="addUser" parameterType="User" useGeneratedKeys="true&qu ...
- sdfs
<!DOCTYPE html><html><head><meta charset="GB18030"><title>In ...
- mysql数据类型和java数据类型匹配
Java数据类型和MySql数据类型对应一览 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java. ...
- delphi异步选择模型编程TCP
Server端: unit U_FrmServer; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, ...
- System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。
错误背景: 操作系统:编程环境:VS2013. 语言:VB.net: 数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...
- FlashBuilder 4.7 非正常关闭导致的不能启动的解决的方法
停电.或者卡死.FB就不能正常启动了. 以下是老外给出的方法,好用: 进入.metadata/.plugins/org.eclipse.core.resources 文件夹 删除.snap文件 假设是 ...
- ABAP学习之旅——多种方式建立模块化功能
在ABAP中.有多种方法能够建立模块化的功能. 以下依次对其种三种进行介绍. 一. 使用子程序(Subroutine) 1. 基本的语法: FORM subname. ...
- Lint工具去除Android工程里不再需要的资源
摘要: 在项目开发过程中常常会不断改UI设计,于是在定稿要发布的前夕,发现有好多不再需要的资源文件存在,发布的包会把这些无用的资源都包含在里面,造成APK的下载包过大.可以通过Android SDK自 ...