Skip to content

Commit 67a6fac

Browse files
committed
feat: add solutions to lc problem: No.0652
No.0652.Find Duplicate Subtrees
1 parent 03adfd0 commit 67a6fac

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,104 @@ func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
195195
}
196196
```
197197

198+
### **TypeScript**
199+
200+
```ts
201+
/**
202+
* Definition for a binary tree node.
203+
* class TreeNode {
204+
* val: number
205+
* left: TreeNode | null
206+
* right: TreeNode | null
207+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
208+
* this.val = (val===undefined ? 0 : val)
209+
* this.left = (left===undefined ? null : left)
210+
* this.right = (right===undefined ? null : right)
211+
* }
212+
* }
213+
*/
214+
215+
function findDuplicateSubtrees(root: TreeNode | null): Array<TreeNode | null> {
216+
const map = new Map<string, number>();
217+
const res = [];
218+
const dfs = (root: TreeNode | null) => {
219+
if (root == null) {
220+
return '#';
221+
}
222+
const { val, left, right } = root;
223+
const s = `${val},${dfs(left)},${dfs(right)}`;
224+
map.set(s, (map.get(s) ?? 0) + 1);
225+
if (map.get(s) === 2) {
226+
res.push(root);
227+
}
228+
return s;
229+
};
230+
dfs(root);
231+
return res;
232+
}
233+
```
234+
235+
### **Rust**
236+
237+
```rust
238+
// Definition for a binary tree node.
239+
// #[derive(Debug, PartialEq, Eq)]
240+
// pub struct TreeNode {
241+
// pub val: i32,
242+
// pub left: Option<Rc<RefCell<TreeNode>>>,
243+
// pub right: Option<Rc<RefCell<TreeNode>>>,
244+
// }
245+
//
246+
// impl TreeNode {
247+
// #[inline]
248+
// pub fn new(val: i32) -> Self {
249+
// TreeNode {
250+
// val,
251+
// left: None,
252+
// right: None
253+
// }
254+
// }
255+
// }
256+
use std::rc::Rc;
257+
use std::cell::RefCell;
258+
use std::collections::HashMap;
259+
impl Solution {
260+
fn dfs(
261+
root: &Option<Rc<RefCell<TreeNode>>>,
262+
map: &mut HashMap<String, i32>,
263+
res: &mut Vec<Option<Rc<RefCell<TreeNode>>>>,
264+
) -> String {
265+
if root.is_none() {
266+
return String::from('#');
267+
}
268+
269+
let s = {
270+
let root = root.as_ref().unwrap().as_ref().borrow();
271+
format!(
272+
"{},{},{}",
273+
root.val.to_string(),
274+
Self::dfs(&root.left, map, res),
275+
Self::dfs(&root.right, map, res)
276+
)
277+
};
278+
*map.entry(s.clone()).or_insert(0) += 1;
279+
if *map.get(&s).unwrap() == 2 {
280+
res.push(root.clone());
281+
}
282+
return s;
283+
}
284+
285+
pub fn find_duplicate_subtrees(
286+
root: Option<Rc<RefCell<TreeNode>>>,
287+
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
288+
let mut map = HashMap::new();
289+
let mut res = Vec::new();
290+
Self::dfs(&root, &mut map, &mut res);
291+
res
292+
}
293+
}
294+
```
295+
198296
### **...**
199297

200298
```

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,104 @@ func findDuplicateSubtrees(root *TreeNode) []*TreeNode {
180180
}
181181
```
182182

183+
### **TypeScript**
184+
185+
```ts
186+
/**
187+
* Definition for a binary tree node.
188+
* class TreeNode {
189+
* val: number
190+
* left: TreeNode | null
191+
* right: TreeNode | null
192+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
193+
* this.val = (val===undefined ? 0 : val)
194+
* this.left = (left===undefined ? null : left)
195+
* this.right = (right===undefined ? null : right)
196+
* }
197+
* }
198+
*/
199+
200+
function findDuplicateSubtrees(root: TreeNode | null): Array<TreeNode | null> {
201+
const map = new Map<string, number>();
202+
const res = [];
203+
const dfs = (root: TreeNode | null) => {
204+
if (root == null) {
205+
return '#';
206+
}
207+
const { val, left, right } = root;
208+
const s = `${val},${dfs(left)},${dfs(right)}`;
209+
map.set(s, (map.get(s) ?? 0) + 1);
210+
if (map.get(s) === 2) {
211+
res.push(root);
212+
}
213+
return s;
214+
};
215+
dfs(root);
216+
return res;
217+
}
218+
```
219+
220+
### **Rust**
221+
222+
```rust
223+
// Definition for a binary tree node.
224+
// #[derive(Debug, PartialEq, Eq)]
225+
// pub struct TreeNode {
226+
// pub val: i32,
227+
// pub left: Option<Rc<RefCell<TreeNode>>>,
228+
// pub right: Option<Rc<RefCell<TreeNode>>>,
229+
// }
230+
//
231+
// impl TreeNode {
232+
// #[inline]
233+
// pub fn new(val: i32) -> Self {
234+
// TreeNode {
235+
// val,
236+
// left: None,
237+
// right: None
238+
// }
239+
// }
240+
// }
241+
use std::rc::Rc;
242+
use std::cell::RefCell;
243+
use std::collections::HashMap;
244+
impl Solution {
245+
fn dfs(
246+
root: &Option<Rc<RefCell<TreeNode>>>,
247+
map: &mut HashMap<String, i32>,
248+
res: &mut Vec<Option<Rc<RefCell<TreeNode>>>>,
249+
) -> String {
250+
if root.is_none() {
251+
return String::from('#');
252+
}
253+
254+
let s = {
255+
let root = root.as_ref().unwrap().as_ref().borrow();
256+
format!(
257+
"{},{},{}",
258+
root.val.to_string(),
259+
Self::dfs(&root.left, map, res),
260+
Self::dfs(&root.right, map, res)
261+
)
262+
};
263+
*map.entry(s.clone()).or_insert(0) += 1;
264+
if *map.get(&s).unwrap() == 2 {
265+
res.push(root.clone());
266+
}
267+
return s;
268+
}
269+
270+
pub fn find_duplicate_subtrees(
271+
root: Option<Rc<RefCell<TreeNode>>>,
272+
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
273+
let mut map = HashMap::new();
274+
let mut res = Vec::new();
275+
Self::dfs(&root, &mut map, &mut res);
276+
res
277+
}
278+
}
279+
```
280+
183281
### **...**
184282

185283
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
use std::collections::HashMap;
22+
impl Solution {
23+
fn dfs(
24+
root: &Option<Rc<RefCell<TreeNode>>>,
25+
map: &mut HashMap<String, i32>,
26+
res: &mut Vec<Option<Rc<RefCell<TreeNode>>>>,
27+
) -> String {
28+
if root.is_none() {
29+
return String::from('#');
30+
}
31+
32+
let s = {
33+
let root = root.as_ref().unwrap().as_ref().borrow();
34+
format!(
35+
"{},{},{}",
36+
root.val.to_string(),
37+
Self::dfs(&root.left, map, res),
38+
Self::dfs(&root.right, map, res)
39+
)
40+
};
41+
*map.entry(s.clone()).or_insert(0) += 1;
42+
if *map.get(&s).unwrap() == 2 {
43+
res.push(root.clone());
44+
}
45+
return s;
46+
}
47+
48+
pub fn find_duplicate_subtrees(
49+
root: Option<Rc<RefCell<TreeNode>>>,
50+
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
51+
let mut map = HashMap::new();
52+
let mut res = Vec::new();
53+
Self::dfs(&root, &mut map, &mut res);
54+
res
55+
}
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function findDuplicateSubtrees(root: TreeNode | null): Array<TreeNode | null> {
16+
const map = new Map<string, number>();
17+
const res = [];
18+
const dfs = (root: TreeNode | null) => {
19+
if (root == null) {
20+
return '#';
21+
}
22+
const { val, left, right } = root;
23+
const s = `${val},${dfs(left)},${dfs(right)}`;
24+
map.set(s, (map.get(s) ?? 0) + 1);
25+
if (map.get(s) === 2) {
26+
res.push(root);
27+
}
28+
return s;
29+
};
30+
dfs(root);
31+
return res;
32+
}

0 commit comments

Comments
 (0)