当前位置:网站首页>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;
}
};
边栏推荐
- 双位置继电器RXMD2-1MRK001984 DC220V
- AcWing 第 57 场周赛---BC题挺好
- Py2neo basic syntax
- 双位置继电器HJWS-9440
- Interview: what are the positioning methods in selenium? Which one do you use most?
- RTP sending PS stream tool (open source)
- [station B up dr_can learning notes] Kalman filter 1
- 微服务系统设计——API 网关服务设计
- Neo4j community conflicts with neo4j desktop
- 微服务系统设计——分布式缓存服务设计
猜你喜欢

系统架构设计——互联网金融的架构设计

Deep dive kotlin synergy (XV): Test kotlin synergy

微服务系统设计——统一鉴权服务设计

Microservice system design -- distributed cache service design

微服务系统设计——API 网关服务设计

什么是BFC?有什么用?

How JQ gets the reciprocal elements

Microservice system design -- API gateway service design

Qt使用Valgrind分析内存泄漏

Pycharm 中 Terminal 无法进入 venv 环境的问题
随机推荐
Pytest框架的执行规则
微信小程序刷新当前页面
ES6 0622 III
Pycharm 中 Terminal 无法进入 venv 环境的问题
DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
Deep dive kotlin synergy (XV): Test kotlin synergy
导航【机器学习】
【NIPS 2017】PointNet++:度量空间中点集的深层次特征学习
018 basics of C language: C file reading and writing
微信小程序WebSocket使用案例
【FPGA】UART串口_V1.1
What is BFC? What's the usage?
How JQ gets the reciprocal elements
双位置继电器RXMD2-1MRK001984 DC220V
019 basics of C language: C preprocessing
Tri rapide (non récursif) et tri de fusion
Microservice system design -- distributed cache service design
Microservice system design -- distributed lock service design
jq怎么获取元素的id名
双位置继电器RXMVB2 R251 204 110DC