|
| 1 | +package org.sean.tree; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +/*** |
| 9 | + * 637. Average of Levels in Binary Tree |
| 10 | + * |
| 11 | + * Input: |
| 12 | + * 3 |
| 13 | + * / \ |
| 14 | + * 9 20 |
| 15 | + * / \ |
| 16 | + * 15 7 |
| 17 | + * Output: [3, 14.5, 11] |
| 18 | + */ |
| 19 | +public class AvgLevelResolver { |
| 20 | + public static int log2(int n) { |
| 21 | + return (int) (Math.log(n) / Math.log(2)); |
| 22 | + } |
| 23 | + |
| 24 | + /*** |
| 25 | + * @see {@link LevelTraversalSolution} |
| 26 | + */ |
| 27 | + public List<List<Integer>> levelOrderBottom(TreeNode root) { |
| 28 | + List<List<Integer>> resultList = new LinkedList<List<Integer>>(); |
| 29 | + if (root == null) { |
| 30 | + return resultList; |
| 31 | + } |
| 32 | + if (root.left == null && root.right == null) { |
| 33 | + List<Integer> list = Arrays.asList(root.val); |
| 34 | + resultList.add(list); |
| 35 | + return resultList; |
| 36 | + } |
| 37 | + |
| 38 | + LinkedList<TreeNode> queue = new LinkedList<>(); |
| 39 | + queue.add(root); |
| 40 | + |
| 41 | + LinkedList<Integer> sequences = new LinkedList<>(); |
| 42 | + sequences.add(1); |
| 43 | + |
| 44 | + int count = 0; |
| 45 | + while (queue.size() > 0) { |
| 46 | + TreeNode node = queue.remove(); |
| 47 | + count = sequences.remove(); |
| 48 | + //++count; |
| 49 | + |
| 50 | + if (node != null) { |
| 51 | + int index = log2(count); |
| 52 | + |
| 53 | + // As the list grows step by step, preserve the order when inserting a group |
| 54 | + if (resultList.size() != 1 + index) { |
| 55 | + List<Integer> lt = new LinkedList<>(); |
| 56 | + lt.add(node.val); |
| 57 | + |
| 58 | + resultList.add(0, lt); |
| 59 | + } else { |
| 60 | + resultList.get(0).add(node.val); |
| 61 | + } |
| 62 | + |
| 63 | + if (node.left != null) { |
| 64 | + sequences.add(count * 2); |
| 65 | + queue.add(node.left); |
| 66 | + } |
| 67 | + |
| 68 | + if (node.right != null) { |
| 69 | + sequences.add(count * 2 + 1); |
| 70 | + queue.add(node.right); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return resultList; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + private void addAvgValue(LinkedList<Double> result, List<Integer> target) { |
| 80 | + int cnt = target.size(); |
| 81 | + |
| 82 | + double total = .0; |
| 83 | + for (Integer i : target) { |
| 84 | + total += i; |
| 85 | + } |
| 86 | + result.add(total / cnt); |
| 87 | + } |
| 88 | + |
| 89 | + public List<Double> averageOfLevels(TreeNode root) { |
| 90 | + LinkedList<Double> result = new LinkedList<>(); |
| 91 | + |
| 92 | + List<List<Integer>> elemList = levelOrderBottom(root); |
| 93 | + |
| 94 | + int len = elemList.size(); |
| 95 | + for (int j = len - 1; j >= 0; j--) { |
| 96 | + List<Integer> list = elemList.get(j); |
| 97 | + |
| 98 | + addAvgValue(result, list); |
| 99 | + } |
| 100 | + |
| 101 | + return result; |
| 102 | + } |
| 103 | +} |
0 commit comments