package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 637. Average of Levels in Binary Tree
 *
 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

 Example 1:
 Input:
  3
 / \
9  20
  /  \
 15   7

 Output: [3, 14.5, 11]
 Explanation:

 The average value of nodes on level 0 is 3,  on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
 Note:
 The range of node's value is in the range of 32-bit signed integer.
 */
public class _637 {

    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            double sum = 0.0;
            for (int i = 0; i < size; i++) {
                TreeNode curr = queue.poll();
                if (curr != null) {
                    sum += curr.val;
                }
                if (curr.left != null) {
                    queue.offer(curr.left);
                }
                if (curr.right != null) {
                    queue.offer(curr.right);
                }
            }
            result.add(sum / size);
        }
        return result;
    }

}

@@ -647,16 +647,56 @@
 _637 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -752,16 +752,20 @@
         
+    
 List%3CDou
@@ -801,24 +801,28 @@
 ();%0A        
+  
+  
 if (root == 
@@ -833,32 +833,36 @@
 ) %7B%0A            
+    
 return result;%0A 
@@ -860,34 +860,42 @@
 result;%0A        
+   
+ 
 %7D%0A
+    
         Queue%3CTr
@@ -922,32 +922,36 @@
 LinkedList%3C%3E();%0A
+    
         queue.of
@@ -969,16 +969,20 @@
         
+    
 while (!
@@ -1012,16 +1012,20 @@
         
+    
 int size
@@ -1053,16 +1053,20 @@
         
+ 
+   
 double s
@@ -1075,16 +1075,20 @@
  = 0.0;%0A
+    
         
@@ -1140,16 +1140,20 @@
         
+   
+ 
 TreeNode
@@ -1182,32 +1182,36 @@
                 
+    
 if (curr != null
@@ -1206,32 +1206,36 @@
 curr != null) %7B%0A
+    
                 
@@ -1263,34 +1263,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -1334,32 +1334,36 @@
                 
+ 
+   
 queue.offer(curr
@@ -1378,34 +1378,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -1402,32 +1402,34 @@
                 
+  
 if (curr.right !
@@ -1430,32 +1430,36 @@
 ight != null) %7B%0A
+    
                 
@@ -1495,34 +1495,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -1519,32 +1519,36 @@
               %7D%0A
+    
             resu
@@ -1567,34 +1567,42 @@
  size);%0A        
-%7D%0A
+    %7D%0A    
         return r
@@ -1604,21 +1604,31 @@
 urn result;%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

refactor 637