当前位置:网站首页>Force buckle 515 Find the maximum value in each tree row
Force buckle 515 Find the maximum value in each tree row
2022-06-28 07:02:00 【Base-Case】
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-largest-value-in-each-tree-row
Ideas : A simple sequence traversal
/**
* 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) {
queue<TreeNode*> q;
vector<int> ans;
int mx;
if(root!=NULL) q.push(root);
while(q.size()){
int len = q.size();// Get the length of the current layer
TreeNode *t = q.front();
mx=t->val;
for(int i=0;i<len;i++){
mx=max(t->val,mx);
if(t->left) q.push(t->left);// Judge whether there are left children
if(t->right) q.push(t->right);// Judge whether there is a right child
q.pop();
t=q.front();
}
ans.push_back(mx);
}
return ans;
}
};边栏推荐
- ice, protobuf ,thrift -- 笔记
- Optimization steps of SQL statements (II) -- MySQL statement optimization
- 代码没写错,渲染页面不显示原因
- Speech enhancement - spectrum mapping
- 【Rust翻譯】從頭實現Rust异步執行器
- Techo day Tencent technology open day, June 28 online waiting for you!
- Reinforcement learning - grid world
- Some habits of it veterans in the workplace
- Is it safe to open a stock trading account on your mobile phone?
- 金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
猜你喜欢

Pytorch RNN learning notes

Design and practice of vivo sensitive word matching system

编译配置in文件

redis的入门学习到起飞,就这一篇搞定

Floating and positioning

From the beginning of redis learning to take-off, this article is all for you

fpm工具安装

VM332 WAService. js:2 Error: _ vm. Changetabs is not a function

RN7302三相电量检测(基于STM32单片机)

Build your jmeter+jenkins+ant
随机推荐
MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
ice, protobuf ,thrift -- 笔记
It will cost 700 yuan to realize this issue. Does anyone do it?
图片按日期批量导入WPS表格
Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio
编译配置in文件
DOM parsing of XML file case code sentence by sentence analysis
【Rust翻譯】從頭實現Rust异步執行器
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
Rust FFI 编程 - libc crate
Servlet value passing JSP
freeswitch使用mod_shout模块播放mp3
Speech enhancement - spectrum mapping
Causes of wechat applet compilation page blank bug
Is it safe to open a stock trading account on your mobile phone?
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
ImportError: cannot import name 'ensure_ dir_ Possible solutions for exists'
2 startup, interrupt and system call
MySQL installation steps - how to create a virtual machine under Linux (1)
Triode driven brushless motor