Submission Detail

66 / 66 test cases passed.
Status:

Accepted

Runtime: 5 ms
Memory Usage: 44.8 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0
2
4
6
8
10
12
14
16
18
10
20
30
40
50
60
java
You are here!
Your runtime beats 10.58 % of java submissions.
Runtime (ms)
Distribution (%)

0
2
4
6
8
10
12
14
16
18
25
50
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

44000
43000
43250
43500
43750
44250
44500
44750
45000
5
10
15
java
You are here!
Your memory usage beats 37.56 % of java submissions.
Memory (KB)
Distribution (%)

44000
43000
43250
43500
43750
44250
44500
44750
45000
5
10
15
Zoom area by dragging across this chart

Invite friends to challenge Average of Levels in Binary Tree


Submitted Code: 0 minutes ago

Language: 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
/**
* Definition for a binary tree node.
* public 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 List<Double> averageOfLevels(TreeNode root) {
List<Double> averages = new LinkedList<>();
LinkedList<TreeNode> queue = new LinkedList<>();
LinkedList<TreeNode> next_queue = new LinkedList<>();
queue.add(root);
while (queue.size() > 0) {
double sum = 0;
for (int i = 0; i < queue.size(); i++) {
TreeNode n = queue.get(i);
sum += n.val;
if (n.left != null)
next_queue.addLast(n.left);
if (n.right != null)
next_queue.addLast(n.right);
}
averages.add(sum/queue.size());
queue = next_queue;
next_queue = new LinkedList<TreeNode>();
}
return averages;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX