|
11 | 11 | import java.util.Queue;
|
12 | 12 | import java.util.Set;
|
13 | 13 |
|
14 |
| - |
15 | 14 | /**
|
16 | 15 | * 652. Find Duplicate Subtrees
|
17 | 16 | *
|
|
37 | 36 | Therefore, you need to return above trees' root in the form of a list.
|
38 | 37 | */
|
39 | 38 | public class _652 {
|
| 39 | + public static class Solution1 { |
40 | 40 |
|
41 |
| - /**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/ |
42 |
| - |
43 |
| - /**You don't actually need to check if every other tree is a duplicate of current node, |
44 |
| - * just when you go through each node, you'll see whether there's already one in the map, |
45 |
| - * since map.containsKey() checks this TreeNode.*/ |
46 |
| - public List<TreeNode> findDuplicateSubtrees(TreeNode root) { |
47 |
| - List<TreeNode> res = new LinkedList<>(); |
48 |
| - postorder(root, new HashMap<>(), res); |
49 |
| - return res; |
50 |
| - } |
51 |
| - |
52 |
| - private String postorder(TreeNode curr, HashMap<String, Integer> map, List<TreeNode> res) { |
53 |
| - if (curr == null) { |
54 |
| - return "#"; |
55 |
| - } |
56 |
| - String serial = curr.val + "," + postorder(curr.left, map, res) + "," + postorder(curr.right, map, res); |
57 |
| - if (map.getOrDefault(serial, 0) == 1) { |
58 |
| - res.add(curr); |
59 |
| - } |
60 |
| - map.put(serial, map.getOrDefault(serial, 0) + 1); |
61 |
| - return serial; |
62 |
| - } |
63 |
| - |
64 |
| - |
65 |
| - public class MyOriginalSolution { |
66 |
| - /**This solution is blocked at [2,1,1] test case and I've asked a question here: |
67 |
| - * https://discuss.leetcode.com/topic/97746/oj-bug-for-test-case-2-1-1-or-somewhere-my-code-is-wrong*/ |
| 41 | + /**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/ |
68 | 42 |
|
69 | 43 | /**
|
70 |
| - * Use BFS to traverse each node, at this time, put each node into Map as key (except root node since root won't have duplicates), |
71 |
| - * then use DFS to visit all of its siblings to find possible duplite subtrees, |
72 |
| - * because duplicate could only possibly be found in siblings or sibling's children. |
| 44 | + * You don't actually need to check if every other tree is a duplicate of current node, |
| 45 | + * just when you go through each node, you'll see whether there's already one in the map, |
| 46 | + * since map.containsKey() checks this TreeNode. |
73 | 47 | */
|
74 | 48 | public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
|
75 |
| - List<TreeNode> result = new ArrayList<>(); |
76 |
| - if (root == null) { |
77 |
| - return result; |
78 |
| - } |
79 |
| - Map<TreeNode, List<TreeNode>> map = new HashMap<>(); |
80 |
| - Queue<TreeNode> oldQueue = new LinkedList<>(); |
81 |
| - Queue<TreeNode> newQueue = new LinkedList<>(); |
82 |
| - oldQueue.offer(root); |
83 |
| - while (!oldQueue.isEmpty()) { |
84 |
| - int size = oldQueue.size(); |
85 |
| - for (int i = 0; i < size; i++) { |
86 |
| - TreeNode curr = oldQueue.poll(); |
87 |
| - if (curr.left != null) { |
88 |
| - newQueue.offer(curr.left); |
89 |
| - } |
90 |
| - if (curr.right != null) { |
91 |
| - newQueue.offer(curr.right); |
92 |
| - } |
93 |
| - if (curr != root) { |
94 |
| - if (!map.containsKey(curr)) { |
95 |
| - map.put(curr, new ArrayList<>()); |
96 |
| - } |
97 |
| - } |
98 |
| - } |
99 |
| - for (TreeNode treeNode : newQueue) { |
100 |
| - findDuplicateSubtrees(treeNode, newQueue, map); |
101 |
| - } |
102 |
| - oldQueue = newQueue; |
103 |
| - } |
104 |
| - Set<TreeNode> set = new HashSet<>(); |
105 |
| - for (Map.Entry<TreeNode, List<TreeNode>> entry : map.entrySet()) { |
106 |
| - if (entry.getValue().size() > 0) { |
107 |
| - set.add(entry.getKey()); |
108 |
| - } |
109 |
| - } |
110 |
| - result.addAll(set); |
111 |
| - return result; |
| 49 | + List<TreeNode> res = new LinkedList<>(); |
| 50 | + postorder(root, new HashMap<>(), res); |
| 51 | + return res; |
112 | 52 | }
|
113 | 53 |
|
114 |
| - private void findDuplicateSubtrees(TreeNode treeNode, Queue<TreeNode> newQueue, Map<TreeNode, List<TreeNode>> map) { |
115 |
| - for (TreeNode tn : newQueue) { |
116 |
| - if (treeNode != tn) { |
117 |
| - if (isSubtree(tn, treeNode)) { |
118 |
| - List<TreeNode> list = map.getOrDefault(treeNode, new ArrayList<>()); |
119 |
| - list.add(tn); |
120 |
| - map.put(treeNode, list); |
121 |
| - return; |
122 |
| - } |
123 |
| - } |
| 54 | + private String postorder(TreeNode curr, HashMap<String, Integer> map, List<TreeNode> res) { |
| 55 | + if (curr == null) { |
| 56 | + return "#"; |
124 | 57 | }
|
125 |
| - } |
126 |
| - |
127 |
| - private boolean isSubtree(TreeNode s, TreeNode t) { |
128 |
| - if (s == null && t == null) { |
129 |
| - return true; |
130 |
| - } |
131 |
| - boolean isSubTree = false; |
132 |
| - if (s != null && t != null && s.val == t.val) { |
133 |
| - isSubTree = isSameTree(s, t); |
134 |
| - } |
135 |
| - if (isSubTree) { |
136 |
| - return true; |
137 |
| - } |
138 |
| - boolean isSubTreeLeft = false; |
139 |
| - if (s.left != null) { |
140 |
| - isSubTreeLeft = isSubtree(s.left, t); |
141 |
| - } |
142 |
| - if (isSubTreeLeft) { |
143 |
| - return true; |
144 |
| - } |
145 |
| - boolean isSubTreeRight = false; |
146 |
| - if (s.right != null) { |
147 |
| - isSubTreeRight = isSubtree(s.right, t); |
148 |
| - } |
149 |
| - if (isSubTreeRight) { |
150 |
| - return true; |
151 |
| - } |
152 |
| - return false; |
153 |
| - } |
154 |
| - |
155 |
| - private boolean isSameTree(TreeNode p, TreeNode q) { |
156 |
| - if (p == null || q == null) { |
157 |
| - return p == q; |
| 58 | + String serial = curr.val + "," + postorder(curr.left, map, res) + "," + postorder(curr.right, map, res); |
| 59 | + if (map.getOrDefault(serial, 0) == 1) { |
| 60 | + res.add(curr); |
158 | 61 | }
|
159 |
| - return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); |
| 62 | + map.put(serial, map.getOrDefault(serial, 0) + 1); |
| 63 | + return serial; |
160 | 64 | }
|
161 | 65 | }
|
162 | 66 | }
|
0 commit comments