-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxLevelSum.java
71 lines (67 loc) · 2 KB
/
MaxLevelSum.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
/*
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: root = [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.
*/
import java.util.*;
public class MaxLevelSum {
public static void main(String[] args) {
Node root=new Node(1);
root.left=new Node(7);
root.right=new Node(0);
root.left.left=new Node(7);
root.left.right=new Node(-8);
root.right.left=null;
root.right.right=null;
int ans=MaxSum(root);
System.out.println(ans);
}
public static int MaxSum(Node root){
ArrayList<Integer> result=new ArrayList<>();
if (root==null){
return 0;
}
Queue<Node> queue=new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int levelsize= queue.size();
int levelsum=0;
for (int i = 0; i < levelsize; i++) {
Node currentnode=queue.poll();
levelsum+= currentnode.data;
if(currentnode.left !=null){
queue.offer(currentnode.left);
}
if (currentnode.right != null){
queue.offer(currentnode.right);
}
}
result.add(levelsum);
}
int maxindex=0;
int max= result.get(0);
for(int i=0; i<result.size(); i++){
int current= result.get(i);
if (current>max){
max=current;
maxindex=i;
}
}
return maxindex+1;
}
}
class Node{
int data;
Node left,right;
public Node(int key){
data=key;
left=right=null;
}
}