-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxLevelSumBinaryTree.java
More file actions
64 lines (51 loc) · 1.97 KB
/
Copy pathMaxLevelSumBinaryTree.java
File metadata and controls
64 lines (51 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package LeetcodePractice;
import java.util.LinkedList;
import java.util.Queue;
public class MaxLevelSumBinaryTree {
public static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public int maxLevelSum(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int level = 1, maxLevel = 1;
int maxSum = Integer.MIN_VALUE;
while (!queue.isEmpty()) {
int size = queue.size();
int currLevelSum = 0;
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
currLevelSum += curr.val;
if (curr.left != null) queue.offer(curr.left);
if (curr.right != null) queue.offer(curr.right);
}
if (currLevelSum > maxSum) {
maxSum = currLevelSum;
maxLevel = level;
}
level++;
}
return maxLevel;
}
// Main method to test the solution
public static void main(String[] args) {
MaxLevelSumBinaryTree solution = new MaxLevelSumBinaryTree();
// Example 1: root = [1,7,0,7,-8,null,null]
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(7);
root1.right = new TreeNode(0);
root1.left.left = new TreeNode(7);
root1.left.right = new TreeNode(-8);
System.out.println("Max Level Sum is at Level: " + solution.maxLevelSum(root1)); // Output: 2
// Example 2: root = [989,null,10250,98693,-89388,null,null,null,-32127]
TreeNode root2 = new TreeNode(989);
root2.right = new TreeNode(10250);
root2.right.left = new TreeNode(98693);
root2.right.right = new TreeNode(-89388);
root2.right.right.right = new TreeNode(-32127);
System.out.println("Max Level Sum is at Level: " + solution.maxLevelSum(root2)); // Output: 2
}
}