|
5 | 5 | import java.util.*;
|
6 | 6 |
|
7 | 7 | /**
|
| 8 | + * 508. Most Frequent Subtree Sum |
| 9 | + * |
8 | 10 | * Given the root of a tree, you are asked to find the most frequent subtree sum.
|
9 | 11 | * The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
|
10 | 12 | * So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.
|
|
28 | 30 | */
|
29 | 31 | public class _508 {
|
30 | 32 |
|
31 |
| - //my purely original but verbose solution |
32 |
| - public int[] findFrequentTreeSum(TreeNode root) { |
33 |
| - if (root == null) return new int[]{}; |
34 |
| - |
35 |
| - Map<TreeNode, Integer> map = new HashMap(); |
36 |
| - postOrder(root, map); |
37 |
| - |
38 |
| - Map<Integer, Integer> frequencyMap = new HashMap<>(); |
39 |
| - for (Map.Entry entry : map.entrySet()) { |
40 |
| - frequencyMap.put((Integer) entry.getValue(), frequencyMap.getOrDefault(entry.getValue(), 0)+1); |
41 |
| - } |
42 |
| - |
43 |
| - List<Map.Entry<Integer, Integer>> list = new LinkedList<>(frequencyMap.entrySet()); |
44 |
| - Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue())); |
45 |
| - |
46 |
| - int mostFrequency = list.get(0).getValue(); |
47 |
| - List<Integer> topFrequencyList = new ArrayList<>(); |
48 |
| - topFrequencyList.add(list.get(0).getKey()); |
49 |
| - int i = 1; |
50 |
| - while (i < list.size() && list.get(i).getValue() == mostFrequency) { |
51 |
| - topFrequencyList.add(list.get(i).getKey()); |
52 |
| - i++; |
| 33 | + public static class Solution1 { |
| 34 | + //my purely original but verbose solution |
| 35 | + public int[] findFrequentTreeSum(TreeNode root) { |
| 36 | + if (root == null) return new int[]{}; |
| 37 | + |
| 38 | + Map<TreeNode, Integer> map = new HashMap(); |
| 39 | + postOrder(root, map); |
| 40 | + |
| 41 | + Map<Integer, Integer> frequencyMap = new HashMap<>(); |
| 42 | + for (Map.Entry entry : map.entrySet()) { |
| 43 | + frequencyMap.put((Integer) entry.getValue(), frequencyMap.getOrDefault(entry.getValue(), 0) + 1); |
| 44 | + } |
| 45 | + |
| 46 | + List<Map.Entry<Integer, Integer>> list = new LinkedList<>(frequencyMap.entrySet()); |
| 47 | + Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue())); |
| 48 | + |
| 49 | + int mostFrequency = list.get(0).getValue(); |
| 50 | + List<Integer> topFrequencyList = new ArrayList<>(); |
| 51 | + topFrequencyList.add(list.get(0).getKey()); |
| 52 | + int i = 1; |
| 53 | + while (i < list.size() && list.get(i).getValue() == mostFrequency) { |
| 54 | + topFrequencyList.add(list.get(i).getKey()); |
| 55 | + i++; |
| 56 | + } |
| 57 | + |
| 58 | + int[] result = new int[topFrequencyList.size()]; |
| 59 | + for (int j = 0; j < topFrequencyList.size(); j++) { |
| 60 | + result[j] = topFrequencyList.get(j); |
| 61 | + } |
| 62 | + |
| 63 | + return result; |
53 | 64 | }
|
54 | 65 |
|
55 |
| - int[] result = new int[topFrequencyList.size()]; |
56 |
| - for (int j = 0; j < topFrequencyList.size(); j++) { |
57 |
| - result[j] = topFrequencyList.get(j); |
| 66 | + private int postOrder(TreeNode root, Map<TreeNode, Integer> map) { |
| 67 | + int left = 0; |
| 68 | + int right = 0; |
| 69 | + if (root.left != null) { |
| 70 | + left = postOrder(root.left, map); |
| 71 | + } |
| 72 | + if (root.right != null) { |
| 73 | + right = postOrder(root.right, map); |
| 74 | + } |
| 75 | + if (root.left == null && root.right == null) { |
| 76 | + map.put(root, root.val); |
| 77 | + return root.val; |
| 78 | + } |
| 79 | + int sum = left + right + root.val; |
| 80 | + map.put(root, sum); |
| 81 | + return sum; |
58 | 82 | }
|
59 |
| - |
60 |
| - return result; |
61 | 83 | }
|
62 | 84 |
|
63 |
| - private int postOrder(TreeNode root, Map<TreeNode, Integer> map) { |
64 |
| - int left = 0; |
65 |
| - int right = 0; |
66 |
| - if (root.left != null) { |
67 |
| - left = postOrder(root.left, map); |
| 85 | + public static class Solution2 { |
| 86 | + //my 2nd purely original but verbose solution |
| 87 | + public int[] findFrequentTreeSum(TreeNode root) { |
| 88 | + Map<Integer, Integer> map = new HashMap<>(); |
| 89 | + dfs(root, map); |
| 90 | + List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(map.entrySet()); |
| 91 | + Collections.sort(entryList, (a, b) -> b.getValue() - a.getValue()); |
| 92 | + List<Integer> list = new ArrayList<>(); |
| 93 | + for (int i = 0; i < entryList.size(); i++) { |
| 94 | + if (list.size() == 0) { |
| 95 | + list.add(entryList.get(i).getKey()); |
| 96 | + } else { |
| 97 | + if (map.get(list.get(0)) == entryList.get(i).getValue()) { |
| 98 | + list.add(entryList.get(i).getKey()); |
| 99 | + } else { |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + int[] result = new int[list.size()]; |
| 105 | + for (int i = 0; i < list.size(); i++) { |
| 106 | + result[i] = list.get(i); |
| 107 | + } |
| 108 | + return result; |
68 | 109 | }
|
69 |
| - if (root.right != null) { |
70 |
| - right = postOrder(root.right, map); |
71 |
| - } |
72 |
| - if (root.left == null && root.right == null) { |
73 |
| - map.put(root, root.val); |
74 |
| - return root.val; |
| 110 | + |
| 111 | + private int dfs(TreeNode root, Map<Integer, Integer> map) { |
| 112 | + if (root == null) { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + if (root.left == null && root.right == null) { |
| 116 | + map.put(root.val, map.getOrDefault(root.val, 0) + 1); |
| 117 | + return root.val; |
| 118 | + } |
| 119 | + int leftVal = 0; |
| 120 | + if (root.left != null) { |
| 121 | + leftVal = dfs(root.left, map); |
| 122 | + } |
| 123 | + int rightVal = 0; |
| 124 | + if (root.right != null) { |
| 125 | + rightVal = dfs(root.right, map); |
| 126 | + } |
| 127 | + int val = leftVal + rightVal + root.val; |
| 128 | + map.put(val, map.getOrDefault(val, 0) + 1); |
| 129 | + return val; |
75 | 130 | }
|
76 |
| - int sum = left + right + root.val; |
77 |
| - map.put(root, sum); |
78 |
| - return sum; |
79 | 131 | }
|
80 | 132 |
|
81 | 133 | //a more concise and space-efficient solution: https://discuss.leetcode.com/topic/77775/verbose-java-solution-postorder-traverse-hashmap-18ms
|
|
0 commit comments