package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/average-of-levels-in-binary-tree/
 */
public class Problem637 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<Double> averageOfLevels(TreeNode root) {
        // TODO
        return new ArrayList<>();
    }

    public static void main(String[] args) {
        Problem637 prob = new Problem637();

        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(prob.averageOfLevels(root)); // [3, 14.5, 11]
    }
}

@@ -57,20 +57,121 @@
 va.util.
-List
+HashMap;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Queue
 ;%0A%0A/**%0A 
@@ -309,19 +309,20 @@
         
-int
+long
  val;%0A  
@@ -500,49 +500,1564 @@
     
-// TODO%0A        return new ArrayList%3C%3E();
+Map%3CInteger, ValueCount%3E map = new HashMap%3C%3E();%0A        Queue%3CLevelNode%3E queue = new LinkedList%3C%3E();%0A        queue.add(new LevelNode(1, root));%0A        while (!queue.isEmpty()) %7B%0A            LevelNode current = queue.remove();%0A            if (!map.containsKey(current.level)) %7B%0A                map.put(current.level, new ValueCount(current.node.val, 1));%0A            %7D else %7B%0A                ValueCount valueCount = map.get(current.level);%0A                valueCount.count++;%0A                valueCount.value += current.node.val;%0A            %7D%0A            if (current.node.left != null) %7B%0A                queue.add(new LevelNode(current.level + 1, current.node.left));%0A            %7D%0A            if (current.node.right != null) %7B%0A                queue.add(new LevelNode(current.level + 1, current.node.right));%0A            %7D%0A        %7D%0A        List%3CDouble%3E result = new ArrayList%3C%3E();%0A        for (int i = 1; i %3C= map.size(); i++) %7B%0A            ValueCount valueCount = map.get(i);%0A            result.add((double) valueCount.value / (double) valueCount.count);%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static class ValueCount %7B%0A        private long value;%0A        private int count;%0A%0A        public ValueCount(long value, int count) %7B%0A            this.value = value;%0A            this.count = count;%0A        %7D%0A    %7D%0A%0A    private static class LevelNode %7B%0A        private final int level;%0A        private final TreeNode node;%0A%0A        public LevelNode(int level, TreeNode node) %7B%0A            this.level = level;%0A            this.node = node;%0A        %7D
 %0A   

Solve problem 637