-
Notifications
You must be signed in to change notification settings - Fork 4
/
FindDuplicateSubtrees.java
50 lines (42 loc) · 1.27 KB
/
FindDuplicateSubtrees.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
package com.dbc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindDuplicateSubtrees {
private int flag = 0;
private final Map<Integer, Integer> counter = new HashMap<>();
private final Map<String, Integer> tree = new HashMap<>();
private final List<TreeNode> res = new ArrayList<>();
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
private String dfs(TreeNode node) {
if (node == null) {
return "null";
}
String serial = String.format("%s-%s-%s", node.val, dfs(node.left), dfs(node.right));
int uid = this.tree.computeIfAbsent(serial, key -> flag++);
this.counter.put(uid, this.counter.getOrDefault(uid, 0) + 1);
if (this.counter.get(uid) == 2) {
this.res.add(node);
}
return serial;
}
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
dfs(root);
return this.res;
}
}