当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
data:image/jpg; base64 format data is converted to image
MySQL大总结
力扣每日一题-第47天-15. 三数之和
[想要访问若依后台]若依框架报错401请求访问:error认证失败,无法访问系统资源
Triton部署mmdeploy导出的TensorRT模型失败篇
登录拦截实现过程
MySQL BIGINT 数据类型
西门子PLC1200与fanuc机器人进行profibus通讯
一天搞定JDBC01:连接数据库并执行sql语句
【深度学习实践(二)】上手手写数字识别
Distributed Computing MapReduce | Spark Experiment
C语言指针
在线问题反馈模块实战(十八):实现excel台账文件记录批量导入功能
curl (7) Failed connect to localhost8080; Connection refused
【字符串】最小表示法
IDEA中创建编写JSP
带你了解一下PHP搭建的电商商城系统
有趣的USB接口和颜色分类
MySQL - Row size too large (> 8126). Changing some columns to TEXT or BLOB
错误记录:TypeError: object() takes no parameters