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

YOLOv5增加小目标检测层

浮动与定位

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

MySQL installation steps - how to create a virtual machine under Linux (1)

Tryout title code

Yolov5 adds a small target detection layer
![[digital statistics DP] counting problem](/img/8d/ac05c1a88543b76fca86cd744e9cb1.jpg)
[digital statistics DP] counting problem

强化学习——格子世界

编译原理期末复习

LeetCode+ 66 - 70 高精度、二分专题
随机推荐
Is it safe to open a stock trading account on your mobile phone?
C语言教程大全
Cmake tips
Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently
Recommend several 0 code, free, learning and using visualization tools
VM332 WAService.js:2 Error: _vm.changeTabs is not a function报错
PLC -- 笔记
The code is correct, and the rendering page does not display the reason
实时数据库 - 笔记
Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
JS regular expression system explanation (comprehensive summary)
Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
【Rust翻译】从头实现Rust异步执行器
推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起
Design and practice of vivo sensitive word matching system
@RequestParam
Will Internet talents be scarce in the future? Which technology directions are popular?
《微信小程序-基础篇》带你了解小程序中的生命周期(一)
How to open UMD, KMD log and dump diagrams in CAMX architecture