当前位置:网站首页>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;
}
};边栏推荐
- Eyebeam advanced settings
- eyebeam高级设置
- freeswitch使用mod_shout模块播放mp3
- fpm工具安装
- 职场IT老鸟的几点小习惯
- [C language] detailed explanation of C language to obtain array length
- FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO
- MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
- 【Rust日报】 2020-04-23 Rust 1.43.0 发布
- [rust daily] published on rust 1.43.0 on April 23, 2020
猜你喜欢
![[online tutorial] official iptables tutorial -- learning notes 1](/img/b9/8f94caa46eb46dab581c713494f36d.png)
[online tutorial] official iptables tutorial -- learning notes 1

强化学习——格子世界

助力涨点 | YOLOv5结合Alpha-IoU

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

Integer promotion and size side byte order
面经---测试工程师web端自动化---大厂面试题

职场IT老鸟的几点小习惯

小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结

Batch import of pictures into WPS table by date

Compile configuration in file
随机推荐
Interpretation of Blog
三极管驱动无刷电机
小程序页面设置100%高度还是留白怎么办?
Servlet value passing JSP
Yolov5 adds a small target detection layer
【星海出品】 运维巡检合集
extern “C“概述
FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources
未来互联网人才还稀缺吗?哪些技术方向热门?
C language tutorial
Recommend several 0 code, free, learning and using visualization tools
Huawei cloud computing physical node cna installation tutorial
LeetCode+ 51 - 55 回溯、动态规划专题
Cmake tips
实现这个 issue 得700块钱人民币,有人做嘛?
[digital statistics DP] counting problem
Triode driven brushless motor
A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages
力扣515.在每棵树行中找最大值
推荐几款0代码、免费、现学现用的可视化工具