-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelWithMaxSum.java
More file actions
49 lines (37 loc) · 967 Bytes
/
Copy pathlevelWithMaxSum.java
File metadata and controls
49 lines (37 loc) · 967 Bytes
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
import java.util.LinkedList;
import java.util.Queue;
// Finding the level that has the maximum sum in the binary tree
public class levelWithMaxSum extends BinaryTree{
public static int findLevelWithMaxSum(Node root) {
Node current = root;
Queue<Node> queue = new LinkedList<Node>();
int level = 0, levelSum = current.data, currentSum = current.data;
if(current == null)
return 0;
else {
queue.offer(current);
queue.offer(null);
}
while(!queue.isEmpty()) {
current = queue.poll();
System.out.println(current.data);
if(queue.isEmpty())
break;
if(current == null) {
level++;
queue.offer(null);
}
else {
currentSum = currentSum + current.data;
if(current.left != null)
queue.add(current.left);
if(current.right != null)
queue.add(current.right);
}
}
return levelSum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}