当前位置:网站首页>LeetCode——1161. 最大层内元素和
LeetCode——1161. 最大层内元素和
2022-08-03 11:03:00 【Cap07】
题目:1161. 最大层内元素和 - 力扣(LeetCode)
package j2;
import java.util.*;
//Definition for a binary tree node.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public int maxLevelSum(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
int max = Integer.MIN_VALUE;
int level = 1;
int answer = 1;
while (!queue.isEmpty()) {
int size = queue.size();
int sum = 0;
for (int i = 0; i < size; i++) {
TreeNode temp = queue.poll();
sum += temp.val;
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
if (sum > max) {
max = sum;
answer = level;
}
level++;
}
return answer;
}
}
public class j3 {
public static void main(String args[]) {
TreeNode T = new TreeNode();
T.val = 1;
T.left = new TreeNode();
T.right = new TreeNode();
T.left.val = 7;
T.right.val = 0;
T.right.left = null;
T.right.right = null;
T.left.left = new TreeNode();
T.left.right = new TreeNode();
T.left.left.val = 7;
T.left.right.val = -8;
Solution S = new Solution();
System.out.println(S.maxLevelSum(T));
}
}边栏推荐
- For invoice processing DocuWare, cast off the yoke of the paper and data input, automatic processing all the invoice received
- 【JDBC以及内部类的讲解】
- 【无标题】函数,对象,方法的区别
- Web Server 设置缓存响应字段的一些推荐方案
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
- 【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制
- What is the ERC20 token standard?
- Machines need tokens more than people
- 白帽黑客与留守儿童破壁对“画”!ISC、中国光华科技基金会、光明网携手启动数字安全元宇宙公益展
- Simple implementation of a high-performance clone of Redis using .NET (1)
猜你喜欢

巴比特 | 元宇宙每日必读:玩家离场,平台关停,数字藏品市场正逐渐降温,行业的未来究竟在哪里?...

【Star项目】小帽飞机大战(九)

SAP 电商云 Spartacus UI 的 External Routes 设计明细

Analysis of the idea of the complete knapsack problem

Web Server 设置缓存响应字段的一些推荐方案

Why is the new earth blurred, in-depth analysis of white balls, viewing pictures, and downloading problems

Activiti产生的背景和作用

for in 和 for of的区别

Simple implementation of a high-performance clone of Redis using .NET (1)

【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制
随机推荐
Dva.js 新手入门指南
完全背包问题的思路解析
Simple implementation of a high-performance clone of Redis using .NET (1)
fast planner中拓扑路径搜索
关于OPENSSL的问题
numpy
[错题]电路维修
微信多开批处理(自动获取安装路径)
Question G: Word Analysis ← Questions for the second provincial competition of the 11th Blue Bridge Cup Competition
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
直播弱网优化
干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换
LeetCode 899 有序队列[字典序] HERODING的LeetCode之路
ARIMA实现(亲测可用)
程序员架构修炼之道:软件架构基本概念和思维
How to retrieve IDC research reports?
【冒泡排序以及奇数偶数排列】
redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)
一文带你弄懂 CDN 技术的原理
3分钟实现内网穿透(基于ngrok实现)