当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

齐纳二极管 稳压二极管 SOD123封装 正负区分

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

认知篇----2022高考志愿该如何填报

Niuke practice 101-c reasoning clown - bit operation + thinking

Microservice system design -- distributed transaction service design
![Golang Hello installation environment exception [resolved]](/img/30/bddba695e4c0059102e86de346b58d.png)
Golang Hello installation environment exception [resolved]

微服务系统设计——服务注册与发现和配置设计

躲避小行星游戏

差点因为 JSON.stringify 丢了奖金...

微服务系统设计——微服务调用设计
随机推荐
双位置继电器RXMVB2 R251 204 110DC
011 C语言基础:C作用域规则
差点因为 JSON.stringify 丢了奖金...
[station B up dr_can learning notes] Kalman filter 2
Py2neo basic syntax
009 basics of C language: C loop
DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
neo4j community与neo4j desktop冲突
【622. 设计循环队列】
Laptop does not have WiFi option solution
[unity] button of UI interactive component & summary of optional base classes
【B站UP DR_CAN学习笔记】Kalman滤波1
013 basics of C language: C pointer
jq怎么获取元素的id名
体验 win10 下 oceanbase 数据库
微服务系统设计——服务熔断和降级设计
Edge在IE模式下加载网页 - Edge设置IE兼容性
012 C language foundation: C array
STM32 MCU pin_ How to configure the pin of single chip microcomputer as pull-up input
RTP sending PS stream tool (open source)