当前位置:网站首页>515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree Row
2022-06-25 12:10:00 【SUNNY_CHANGQI】

The first time to pass in once try, congratulate me
/** * Definition for a binary tree node. * 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 {
};
}
queue<TreeNode *> q;
vector<int> res;
q.push(root);
while(!q.empty()){
int len = q.size();
int max_val = INT_MIN;
while (len){
TreeNode *tmp = q.front();
max_val = max(tmp->val, max_val);
q.pop();
--len;
if (tmp->left) {
q.push(tmp->left);
}
if (tmp->right) {
q.push(tmp->right);
}
}
res.emplace_back(max_val);
}
return res;
}
};
边栏推荐
- Go novice exploration road 2
- JS function exercises
- 2021-09-28
- Concat(), join(), reverse(), sort() method in JS array
- Optimal solution for cold start
- 地理空间搜索:kd树的实现原理
- PHP replaces the key of a two-dimensional array with a specified element value
- PHP numeric array sorting and associative array sorting
- 2021-09-30
- 最大数[抽象排序之抽象规则]
猜你喜欢

PPT绘论文图之导出分辨率

(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code

laravel 9

Penetration tool environment -- use of cknife Chinese kitchen knife

地理空间搜索:kd树的实现原理

冷启动的最优解决方案

聊聊高可用的 11 个关键技巧

The server reported an error 503 service unavailable:the system returned: (71) protocol error

Go from 0 to 1. Obtain the installation package, get, post request, params, body and other parameters

Execution order of MySQL query statements join, on and where
随机推荐
初识CANOpen
Shell learning notes (latest update: 2022-02-18)
PHP files running online
MySQL and excel tables importing database data (Excel for MySQL)
ECSHOP product attribute color specification size stock item No. automatic combination
Go novice exploration road 2
Laravel task scheduling
2021-09-30
2021-09-28
How can we differ LEFT OUTER JOIN vs Left Join [duplicate]
2021-10-21
yolov5训练使用的负样本图片
ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function
Summary of common MySQL database commands (from my own view)
Maximum number [abstract rules for abstract sorting]
PHP takes the difference set of two arrays
Embedded software development written examination and interview notes (latest update: February 17, 2022)
聊聊高可用的 11 个关键技巧
ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping
(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code