-
Notifications
You must be signed in to change notification settings - Fork 0
/
AverageOfLevels.java
35 lines (32 loc) · 1.19 KB
/
AverageOfLevels.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
class AverageOfLevels {
public List<Double> averageOfLevels(TreeNode root) {
//Checking the base condition
if(root==null)
{
return new ArrayList<Double>(){};
}
List<Double> result=new ArrayList<Double>();
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(root); // Initially adding root to the queue to check for the left and right part
while(!queue.isEmpty())
{
double sum=0;
int size=queue.size(); // Check the size and dequeue the elements from queue
for(int i=0;i<size;i++)
{
TreeNode value=queue.poll();
sum+=value.val;
if(value.left!=null) //Add the left node to the queue to check the left subtree
{
queue.add(value.left);
}
if(value.right!=null)//Add the right node to the queue to check the right subtree
{
queue.add(value.right);
}
}
result.add(sum/size); // Adding the average to the list
}
return result;
}
}