-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1161. Maximum Level Sum of a Binary Tree.java
executable file
·75 lines (58 loc) · 1.63 KB
/
1161. Maximum Level Sum of a Binary Tree.java
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
65
66
67
68
69
70
71
72
73
74
75
M
tags: Graph, BFS, DFS
time: O(n) visit all nodes
space: O(n)
#### BFS
- simply calc each level sum with BFS
- top-level is processed first, since we go from top level -> deeper level
- only update result if sum is truly > global MAX.
```
/*
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
Example 1:
Input: [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
Note:
The number of nodes in the given tree is between 1 and 10^4.
-10^5 <= node.val <= 10^5
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = Integer.MIN_VALUE;
public int maxLevelSum(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int result = 0, level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
int sum = 0;
level++;
while (size-- > 0) {
TreeNode node = queue.poll();
sum += node.val;
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
if (sum > max) {
max = sum;
result = level;
}
}
return result;
}
}
```