forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_652.java
162 lines (146 loc) · 5.46 KB
/
_652.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/**
* 652. Find Duplicate Subtrees
*
* Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1
/ \
2 3
/ / \
4 2 4
/
4
The following are two duplicate subtrees:
2
/
4
and
4
Therefore, you need to return above trees' root in the form of a list.
*/
public class _652 {
/**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/
/**You don't actually need to check if every other tree is a duplicate of current node,
* just when you go through each node, you'll see whether there's already one in the map,
* since map.containsKey() checks this TreeNode.*/
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> res = new LinkedList<>();
postorder(root, new HashMap<>(), res);
return res;
}
private String postorder(TreeNode curr, HashMap<String, Integer> map, List<TreeNode> res) {
if (curr == null) {
return "#";
}
String serial = curr.val + "," + postorder(curr.left, map, res) + "," + postorder(curr.right, map, res);
if (map.getOrDefault(serial, 0) == 1) {
res.add(curr);
}
map.put(serial, map.getOrDefault(serial, 0) + 1);
return serial;
}
public class MyOriginalSolution {
/**This solution is blocked at [2,1,1] test case and I've asked a question here:
* https://discuss.leetcode.com/topic/97746/oj-bug-for-test-case-2-1-1-or-somewhere-my-code-is-wrong*/
/**
* 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),
* then use DFS to visit all of its siblings to find possible duplite subtrees,
* because duplicate could only possibly be found in siblings or sibling's children.
*/
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> result = new ArrayList<>();
if (root == null) {
return result;
}
Map<TreeNode, List<TreeNode>> map = new HashMap<>();
Queue<TreeNode> oldQueue = new LinkedList<>();
Queue<TreeNode> newQueue = new LinkedList<>();
oldQueue.offer(root);
while (!oldQueue.isEmpty()) {
int size = oldQueue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = oldQueue.poll();
if (curr.left != null) {
newQueue.offer(curr.left);
}
if (curr.right != null) {
newQueue.offer(curr.right);
}
if (curr != root) {
if (!map.containsKey(curr)) {
map.put(curr, new ArrayList<>());
}
}
}
for (TreeNode treeNode : newQueue) {
findDuplicateSubtrees(treeNode, newQueue, map);
}
oldQueue = newQueue;
}
Set<TreeNode> set = new HashSet<>();
for (Map.Entry<TreeNode, List<TreeNode>> entry : map.entrySet()) {
if (entry.getValue().size() > 0) {
set.add(entry.getKey());
}
}
result.addAll(set);
return result;
}
private void findDuplicateSubtrees(TreeNode treeNode, Queue<TreeNode> newQueue, Map<TreeNode, List<TreeNode>> map) {
for (TreeNode tn : newQueue) {
if (treeNode != tn) {
if (isSubtree(tn, treeNode)) {
List<TreeNode> list = map.getOrDefault(treeNode, new ArrayList<>());
list.add(tn);
map.put(treeNode, list);
return;
}
}
}
}
private boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null && t == null) {
return true;
}
boolean isSubTree = false;
if (s != null && t != null && s.val == t.val) {
isSubTree = isSameTree(s, t);
}
if (isSubTree) {
return true;
}
boolean isSubTreeLeft = false;
if (s.left != null) {
isSubTreeLeft = isSubtree(s.left, t);
}
if (isSubTreeLeft) {
return true;
}
boolean isSubTreeRight = false;
if (s.right != null) {
isSubTreeRight = isSubtree(s.right, t);
}
if (isSubTreeRight) {
return true;
}
return false;
}
private boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null || q == null) {
return p == q;
}
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}