当前位置:网站首页>1161. Maximum Level Sum of a Binary Tree
1161. Maximum Level Sum of a Binary Tree
2022-08-04 06:55:00 【SUNNY_CHANGQI】
The description of the problem
Given the root of a binary tree, the level of its roots is 1, the level of its children is 2, and so on.
Return the smallest level x such that the sum of all the value of nodes at level x is maximal.
The intuition for this problem
- traverse the element in the binary tree by levels.
- log the max sum value and its index level
The codes
#include <iostream>
#include <queue>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {
}
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
}
};
class Solution {
public:
int maxLevelSum(TreeNode* root) {
if(!root) return 0;
int target_level = 0;
int max_sum = INT_MIN;
int level = 0;
std::queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
++level;
int n = q.size();
int level_sum = 0;
for (int i = 0; i < n; i++) {
TreeNode* node = q.front();
q.pop();
level_sum += node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
if (level_sum > max_sum) {
max_sum = level_sum;
target_level = level;
}
}
return target_level;
}
};
int main() {
Solution s;
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
std::cout << s.maxLevelSum(root) << std::endl;
return 0;
}
the corresponding results

The codes using dfs
/** * 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:
int maxLevelSum(TreeNode* root) {
if(!root) return 0;
int target_level = 0;
int max_sum = INT_MIN;
int level = 0;
std::queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
++level;
int n = q.size();
int level_sum = 0;
for (int i = 0; i < n; i++) {
TreeNode* node = q.front();
q.pop();
level_sum += node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
if (level_sum > max_sum) {
max_sum = level_sum;
target_level = level;
}
}
return target_level;
}
};
The corresponding results

边栏推荐
- Distributed Computing Experiment 2 Thread Pool
- 开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
- 【愚公系列】2022年07月 Go教学课程 027-深拷贝和浅拷贝
- Use of MotionLayout
- 一天学会JDBC06:PrepaerdStatemtnt
- Secondary network security competition C module MS17-010 batch scanning
- The national vocational skills contest competition of network security emergency response
- 53个全球免费学术资源数据库整理,查资料写论文必备【开学必备】
- Distributed Computing Experiment 3 PRC-based Book Information Management System
- CAN协议详解-01
猜你喜欢
随机推荐
分布式计算实验2 线程池
国内外知名源码商城系统盘点
【字符串】最小表示法
LeetCode 97. 交错字符串
Error occurred while trying to proxy request项目突然起不来了
解决:Hbuilder工具点击发行打包,一直报尚未完成社区身份验证,请点击链接xxxxx,项目xxx发布H5失败的错误。
带你了解一下PHP搭建的电商商城系统
经典新诗九首
MySQL配置文件配置
curl (7) Failed connect to localhost8080; Connection refused
MotionLayout的使用
MySQL重置root密码
The national vocational skills contest competition of network security emergency response
一天学会JDBC06:PrepaerdStatemtnt
【并发】概念
MySQL基础(DDL、DML、DQL)
【深度学习实践(二)】上手手写数字识别
卷积神经网络CNN
redis stream 实现消息队列
CSRF和SSRF漏洞









