当前位置:网站首页>LeetCode-515. Find the maximum value in each tree row
LeetCode-515. Find the maximum value in each tree row
2022-06-27 05:17:00 【Border wanderer】
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example 1:

Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Example 2:
Input : root = [1,2,3]
Output : [1,3]
Tips :
The range of the number of nodes in a binary tree is [0,104]
-231 <= Node.val <= 231 - 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) {
return {};
}
vector<int> res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int len = q.size();
int maxVal = INT_MIN;
while (len--) {
TreeNode* t = q.front();
q.pop();
maxVal = maxVal > t->val ? maxVal : t->val;
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
res.push_back(maxVal);
}
return res;
}
};
边栏推荐
- 【FPGA】UART串口_V1.1
- 007 basics of C language: C operator
- EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
- 微服务系统设计——服务注册与发现和配置设计
- Interview: what are the positioning methods in selenium? Which one do you use most?
- Common programming abbreviations for orbit attitude
- DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
- What is BFC? What's the usage?
- pycharm 如何安装 package
- 洛谷P4683 [IOI2008] Type Printer 题解
猜你喜欢

leetcode-20. Valid parentheses -js version

双位置继电器DLS-34A DC0.5A 220VDC

Redis高可用集群(哨兵、集群)
![Golang Hello installation environment exception [resolved]](/img/30/bddba695e4c0059102e86de346b58d.png)
Golang Hello installation environment exception [resolved]

【B站UP DR_CAN学习笔记】Kalman滤波1

Tsinghua University open source software mirror website

双位置继电器HJWS-9440

Microservice system design - service fusing and degradation design

Edge loads web pages in IE mode - edge sets ie compatibility
![[station B up dr_can learning notes] Kalman filter 3](/img/40/d3ec97be2f29b76a6c049c26ff4998.gif)
[station B up dr_can learning notes] Kalman filter 3
随机推荐
leetcode299周赛记录
014 C语言基础:C字符串
014 C language foundation: C string
Experience oceanbase database under win10
golang hello 安装环境异常【已解决】
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
neo4j图数据库基本概念
neo4j community与neo4j desktop冲突
RTP 发送PS流工具(已经开源)
Redis高可用集群(哨兵、集群)
Microservice system design -- service registration, discovery and configuration design
017 basics of C language: bit field and typedef
EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
微服务系统设计——分布式锁服务设计
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
体验 win10 下 oceanbase 数据库
微服务系统设计——微服务调用设计
three.js第一人称 相机前枪的跟随
ES6 0622 III
Flink生产问题(1.10)