-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-652.java
More file actions
36 lines (36 loc) · 1.08 KB
/
lc-652.java
File metadata and controls
36 lines (36 loc) · 1.08 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private Set<String> paths = new HashSet();
//private Set<TreeNode> res = new HashSet();
private Map<String, TreeNode> Res = new HashMap();
public List<TreeNode> findDuplicateSubtrees(TreeNode r) {
//走一遍后续遍历?试试
checkDuplicate(r);
return new ArrayList(Res.values());
}
private String checkDuplicate(TreeNode root) {
if(root == null) return null;
else {
String rootp = "";
String lp = checkDuplicate(root.left);
String rp = checkDuplicate(root.right);
rootp += lp == null?"":lp+"L";
rootp += root.val;
rootp += rp == null?"":"R"+rp;
//System.out.println(rootp);
if(paths.contains(rootp) && !Res.containsKey(rootp)){
Res.put(rootp, root);
}
else paths.add(rootp);
return rootp;
}
}
}