当前位置:网站首页>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;
}
};边栏推荐
- Rust FFI programming - libc crate
- Tryout title code
- Huawei cloud computing physical node cna installation tutorial
- 4~20ma input /0~5v output i/v conversion circuit
- 华为云计算之物理节点CNA安装教程
- VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
- Will Internet talents be scarce in the future? Which technology directions are popular?
- 强化学习——格子世界
- 力扣515.在每棵树行中找最大值
- Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)
猜你喜欢

What is a consistent hash? What scenarios can it be applied to?

全方位透析真实企业软件测试流程

Overview, implementation and use of CRC32

助力涨点 | YOLOv5结合Alpha-IoU

My MVVM open source project "travel epidemic prevention app" has been released
Face to face experience --- test engineer web side automation --- interview questions for large factories

Niubi 666, this project makes web page making as simple as building blocks

Some habits of it veterans in the workplace

js正则表达式系统讲解(全面的总结)

How bacnet/ip gateway collects data of building centralized control system
随机推荐
Rust FFI programming - libc crate
Compilation principles final review
Some habits of it veterans in the workplace
Is it safe to open a stock trading account on your mobile phone?
Techo Day 腾讯技术开放日,6月28日线上等你!
Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
Pfizer's new Guankou medicine has entered the Chinese market, and the listing of relevant products of domestic pharmaceutical enterprises is just around the corner
Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently
小程序页面设置100%高度还是留白怎么办?
【星海出品】 运维巡检合集
华为云计算之物理节点CNA安装教程
[rust daily] published on rust 1.43.0 on April 23, 2020
JS regular expression system explanation (comprehensive summary)
编译配置in文件
What is a consistent hash? What scenarios can it be applied to?
YOLOv5增加小目标检测层
CMAKE小知识
Libuv framework echo server C source code explanation (TCP part)
Wechat applets - basics takes you to understand the life cycle of applets (I)
How bacnet/ip gateway collects data of building centralized control system