当前位置:网站首页>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
边栏推荐
- 一天学会JDBC06:PrepaerdStatemtnt
- 带你了解一下PHP搭建的电商商城系统
- Amazon亚马逊 Vendor Central Label详解
- 经典新诗九首
- 分布式计算实验4 随机信号分析系统
- Lightweight Backbone VGNetG Achieves "No Choice, All" Lightweight Backbone Network
- Distributed Computing Experiment 3 PRC-based Book Information Management System
- IDEA中创建编写JSP
- SQL去重的三种方法汇总
- Secondary network security competition C module MS17-010 batch scanning
猜你喜欢
随机推荐
MySQL内存淘汰策略
Transform 相对位置变换,坐标系转换
New Questions in Module B of Secondary Vocational Network Security Competition
Use of MotionLayout
创建数据库报错--MySQL server is running with the --super-read-only option
一天学会JDBC04:ResultSet的用法
打破千篇一律,DIY属于自己独一无二的商城
国内外知名源码商城系统盘点
redis---分布式锁存在的问题及解决方案(Redisson)
Activiti 工作流引擎 详解
全国职业院校技能大赛网络安全竞赛之应急响应
pycharm专业版使用
curl (7) Failed connect to localhost8080; Connection refused
ubuntu18.04安装redis教程
一天搞定JDBC01:连接数据库并执行sql语句
核心价值观编码器【matlab版】
【学习笔记】AGC036
使用腾讯云发送短信 ---- 手把手教你搞定所有步骤
七夕送礼,心愿直抵!
redis stream 实现消息队列