Skip to content

Commit db89880

Browse files
committed
feat: add solutions to lc problem: No.0652
No.0652.Find Duplicate Subtrees
1 parent cd81da7 commit db89880

File tree

6 files changed

+354
-13
lines changed

6 files changed

+354
-13
lines changed

solution/0600-0699/0652.Find Duplicate Subtrees/README.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,146 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
后序遍历,序列化每个子树,用哈希表判断序列化的字符串出现次数是否等于 2,若是,说明这棵子树重复。
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

4648
<!-- 这里可写当前语言的特殊实现逻辑 -->
4749

4850
```python
49-
51+
# Definition for a binary tree node.
52+
# class TreeNode:
53+
# def __init__(self, val=0, left=None, right=None):
54+
# self.val = val
55+
# self.left = left
56+
# self.right = right
57+
class Solution:
58+
def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
59+
def dfs(root):
60+
if root is None:
61+
return '#'
62+
v = f'{root.val},{dfs(root.left)},{dfs(root.right)}'
63+
counter[v] += 1
64+
if counter[v] == 2:
65+
ans.append(root)
66+
return v
67+
68+
ans = []
69+
counter = Counter()
70+
dfs(root)
71+
return ans
5072
```
5173

5274
### **Java**
5375

5476
<!-- 这里可写当前语言的特殊实现逻辑 -->
5577

5678
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
private Map<String, Integer> counter;
96+
private List<TreeNode> ans;
97+
98+
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
99+
counter = new HashMap<>();
100+
ans = new ArrayList<>();
101+
dfs(root);
102+
return ans;
103+
}
104+
105+
private String dfs(TreeNode root) {
106+
if (root == null) {
107+
return "#";
108+
}
109+
String v = root.val + "," + dfs(root.left) + "," + dfs(root.right);
110+
counter.put(v, counter.getOrDefault(v, 0) + 1);
111+
if (counter.get(v) == 2) {
112+
ans.add(root);
113+
}
114+
return v;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
/**
123+
* Definition for a binary tree node.
124+
* struct TreeNode {
125+
* int val;
126+
* TreeNode *left;
127+
* TreeNode *right;
128+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
129+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
130+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
unordered_map<string, int> counter;
136+
vector<TreeNode*> ans;
137+
138+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
139+
dfs(root);
140+
return ans;
141+
}
142+
143+
string dfs(TreeNode* root) {
144+
if (!root) return "#";
145+
string v = to_string(root->val) + "," + dfs(root->left) + "," + dfs(root->right);
146+
++counter[v];
147+
if (counter[v] == 2) ans.push_back(root);
148+
return v;
149+
}
150+
};
151+
```
57152
153+
### **Go**
154+
155+
```go
156+
/**
157+
* Definition for a binary tree node.
158+
* type TreeNode struct {
159+
* Val int
160+
* Left *TreeNode
161+
* Right *TreeNode
162+
* }
163+
*/
164+
func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
165+
var ans []*TreeNode
166+
counter := make(map[string]int)
167+
var dfs func(root *TreeNode) string
168+
dfs = func(root *TreeNode) string {
169+
if root == nil {
170+
return "#"
171+
}
172+
v := strconv.Itoa(root.Val) + "," + dfs(root.Left) + "," + dfs(root.Right)
173+
counter[v]++
174+
if counter[v] == 2 {
175+
ans = append(ans, root)
176+
}
177+
return v
178+
}
179+
dfs(root)
180+
return ans
181+
}
58182
```
59183

60184
### **...**

solution/0600-0699/0652.Find Duplicate Subtrees/README_EN.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,135 @@
4747
### **Python3**
4848

4949
```python
50-
50+
# Definition for a binary tree node.
51+
# class TreeNode:
52+
# def __init__(self, val=0, left=None, right=None):
53+
# self.val = val
54+
# self.left = left
55+
# self.right = right
56+
class Solution:
57+
def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
58+
def dfs(root):
59+
if root is None:
60+
return '#'
61+
v = f'{root.val},{dfs(root.left)},{dfs(root.right)}'
62+
counter[v] += 1
63+
if counter[v] == 2:
64+
ans.append(root)
65+
return v
66+
67+
ans = []
68+
counter = Counter()
69+
dfs(root)
70+
return ans
5171
```
5272

5373
### **Java**
5474

5575
```java
76+
/**
77+
* Definition for a binary tree node.
78+
* public class TreeNode {
79+
* int val;
80+
* TreeNode left;
81+
* TreeNode right;
82+
* TreeNode() {}
83+
* TreeNode(int val) { this.val = val; }
84+
* TreeNode(int val, TreeNode left, TreeNode right) {
85+
* this.val = val;
86+
* this.left = left;
87+
* this.right = right;
88+
* }
89+
* }
90+
*/
91+
class Solution {
92+
private Map<String, Integer> counter;
93+
private List<TreeNode> ans;
94+
95+
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
96+
counter = new HashMap<>();
97+
ans = new ArrayList<>();
98+
dfs(root);
99+
return ans;
100+
}
101+
102+
private String dfs(TreeNode root) {
103+
if (root == null) {
104+
return "#";
105+
}
106+
String v = root.val + "," + dfs(root.left) + "," + dfs(root.right);
107+
counter.put(v, counter.getOrDefault(v, 0) + 1);
108+
if (counter.get(v) == 2) {
109+
ans.add(root);
110+
}
111+
return v;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
/**
120+
* Definition for a binary tree node.
121+
* struct TreeNode {
122+
* int val;
123+
* TreeNode *left;
124+
* TreeNode *right;
125+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
127+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
128+
* };
129+
*/
130+
class Solution {
131+
public:
132+
unordered_map<string, int> counter;
133+
vector<TreeNode*> ans;
134+
135+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
136+
dfs(root);
137+
return ans;
138+
}
139+
140+
string dfs(TreeNode* root) {
141+
if (!root) return "#";
142+
string v = to_string(root->val) + "," + dfs(root->left) + "," + dfs(root->right);
143+
++counter[v];
144+
if (counter[v] == 2) ans.push_back(root);
145+
return v;
146+
}
147+
};
148+
```
56149
150+
### **Go**
151+
152+
```go
153+
/**
154+
* Definition for a binary tree node.
155+
* type TreeNode struct {
156+
* Val int
157+
* Left *TreeNode
158+
* Right *TreeNode
159+
* }
160+
*/
161+
func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
162+
var ans []*TreeNode
163+
counter := make(map[string]int)
164+
var dfs func(root *TreeNode) string
165+
dfs = func(root *TreeNode) string {
166+
if root == nil {
167+
return "#"
168+
}
169+
v := strconv.Itoa(root.Val) + "," + dfs(root.Left) + "," + dfs(root.Right)
170+
counter[v]++
171+
if counter[v] == 2 {
172+
ans = append(ans, root)
173+
}
174+
return v
175+
}
176+
dfs(root)
177+
return ans
178+
}
57179
```
58180

59181
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
unordered_map<string, int> counter;
15+
vector<TreeNode*> ans;
16+
17+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
18+
dfs(root);
19+
return ans;
20+
}
21+
22+
string dfs(TreeNode* root) {
23+
if (!root) return "#";
24+
string v = to_string(root->val) + "," + dfs(root->left) + "," + dfs(root->right);
25+
++counter[v];
26+
if (counter[v] == 2) ans.push_back(root);
27+
return v;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
10+
var ans []*TreeNode
11+
counter := make(map[string]int)
12+
var dfs func(root *TreeNode) string
13+
dfs = func(root *TreeNode) string {
14+
if root == nil {
15+
return "#"
16+
}
17+
v := strconv.Itoa(root.Val) + "," + dfs(root.Left) + "," + dfs(root.Right)
18+
counter[v]++
19+
if counter[v] == 2 {
20+
ans = append(ans, root)
21+
}
22+
return v
23+
}
24+
dfs(root)
25+
return ans
26+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
17+
private Map<String, Integer> counter;
18+
private List<TreeNode> ans;
19+
220
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
3-
List<TreeNode> res = new ArrayList<>();
4-
dfs(root, new HashMap<>(), res);
5-
return res;
21+
counter = new HashMap<>();
22+
ans = new ArrayList<>();
23+
dfs(root);
24+
return ans;
625
}
726

8-
private String dfs(TreeNode root, Map<String, Integer> map, List<TreeNode> res) {
27+
private String dfs(TreeNode root) {
928
if (root == null) {
10-
return "";
29+
return "#";
1130
}
12-
String s = root.val + "_" + dfs(root.left, map, res) + "_" + dfs(root.right, map, res);
13-
map.put(s, map.getOrDefault(s, 0) + 1);
14-
if (map.get(s) == 2) {
15-
res.add(root);
31+
String v = root.val + "," + dfs(root.left) + "," + dfs(root.right);
32+
counter.put(v, counter.getOrDefault(v, 0) + 1);
33+
if (counter.get(v) == 2) {
34+
ans.add(root);
1635
}
17-
return s;
36+
return v;
1837
}
19-
}
38+
}

0 commit comments

Comments
 (0)