当前位置:网站首页>417 sequence traversal of binary tree 1 (102. sequence traversal of binary tree, 107. level traversal of binary tree II, 199. right view of binary tree, 637. layer average of binary tree)
417 sequence traversal of binary tree 1 (102. sequence traversal of binary tree, 107. level traversal of binary tree II, 199. right view of binary tree, 637. layer average of binary tree)
2022-06-25 08:10:00 【liufeng2023】
102. Sequence traversal of binary tree

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
vector<vector<int>> res;
while (!que.empty())
{
int size = que.size();
vector<int> temp;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
temp.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(temp);
}
return res;
}
};

107. The level traversal of binary tree II

class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> que;
vector<vector<int>> res;
if (root != nullptr) que.push(root);
while (!que.empty())
{
int size = que.size();
vector<int> temp;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
temp.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(temp);
}
reverse(res.begin(), res.end());
return res;
}
};

199. Right side view of binary tree

class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<int> result;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (i == (size - 1)) result.push_back(node->val); // Put the last element of each layer into result Array
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};

637. The layer average of a binary tree

class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<double> result;
while (!que.empty()) {
int size = que.size();
double sum = 0; // Count the sum of each layer
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
sum += node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(sum / size); // Put the mean of each layer into the result set
}
return result;
}
};

边栏推荐
- 【莫比乌斯反演】
- TCP and UDP
- 使用pytorch搭建MobileNetV2并基于迁移学习训练
- Ffmpeg+sdl2 for audio playback
- bat启动.NET Core
- To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
- Debugging mipi-dsi screen based on stm32mp157
- Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
- 自制坡道,可是真的很香
- 2265. number of nodes with statistical value equal to the average value of subtree
猜你喜欢

TCP stuff

How do I install the software using the apt get command?

取消word文档中某些页面的页眉

电子学:第013课——实验 14:可穿戴的脉冲发光体

c#搭建ftp服务器并实现文件上传和下载

Mining microbial dark matter -- a new idea

Electronics: Lesson 011 - experiment 10: transistor switches

Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries

Electronics: Lesson 012 - Experiment 11: light and sound

Solving some interesting problems with recurrence of function
随机推荐
Luogu p5994 [pa2014]kuglarz (XOR thinking +mst)
Pycharm的奇葩设定:取消注释后立马复制会带上#
bat启动.NET Core
Neural network and deep learning-3-simple example of machine learning pytorch
Can bus working condition and signal quality "physical examination"
【补题】2021牛客暑期多校训练营1-3
Machine learning notes linear regression of time series
电子学:第012课——实验 11:光和声
TCP的那点玩意儿
自制坡道,可是真的很香
Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
How to create a new branch with SVN
Electronics: Lesson 008 - Experiment 6: very simple switches
50. pow (x, n) - fast power
PH neutralization process modeling
Is it safe to open an account through the haircut account opening link now?
MySQL simple permission management
TCP stuff
Electronics: Lesson 010 - Experiment 8: relay oscillator
Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer