Skip to content

Commit 34f97b5

Browse files
committed
feat: add solutions to lc problem: No.0508
No.0508.Most Frequent Subtree Sum
1 parent cb6c4f9 commit 34f97b5

File tree

4 files changed

+278
-0
lines changed

4 files changed

+278
-0
lines changed

solution/0500-0599/0508.Most Frequent Subtree Sum/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,102 @@ func findFrequentTreeSum(root *TreeNode) []int {
203203
}
204204
```
205205

206+
### **TypeScript**
207+
208+
```ts
209+
/**
210+
* Definition for a binary tree node.
211+
* class TreeNode {
212+
* val: number
213+
* left: TreeNode | null
214+
* right: TreeNode | null
215+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
216+
* this.val = (val===undefined ? 0 : val)
217+
* this.left = (left===undefined ? null : left)
218+
* this.right = (right===undefined ? null : right)
219+
* }
220+
* }
221+
*/
222+
223+
function findFrequentTreeSum(root: TreeNode | null): number[] {
224+
const map = new Map<number, number>();
225+
let max = 0;
226+
const dfs = (root: TreeNode | null) => {
227+
if (root == null) {
228+
return 0;
229+
}
230+
const { val, left, right } = root;
231+
const sum = val + dfs(left) + dfs(right);
232+
map.set(sum, (map.get(sum) ?? 0) + 1);
233+
max = Math.max(max, map.get(sum));
234+
return sum;
235+
};
236+
dfs(root);
237+
const res = [];
238+
for (const [k, v] of map) {
239+
if (v === max) {
240+
res.push(k);
241+
}
242+
}
243+
return res;
244+
}
245+
```
246+
247+
### **Rust**
248+
249+
```rust
250+
// Definition for a binary tree node.
251+
// #[derive(Debug, PartialEq, Eq)]
252+
// pub struct TreeNode {
253+
// pub val: i32,
254+
// pub left: Option<Rc<RefCell<TreeNode>>>,
255+
// pub right: Option<Rc<RefCell<TreeNode>>>,
256+
// }
257+
//
258+
// impl TreeNode {
259+
// #[inline]
260+
// pub fn new(val: i32) -> Self {
261+
// TreeNode {
262+
// val,
263+
// left: None,
264+
// right: None
265+
// }
266+
// }
267+
// }
268+
use std::rc::Rc;
269+
use std::cell::RefCell;
270+
use std::collections::HashMap;
271+
impl Solution {
272+
fn dfs(
273+
root: &Option<Rc<RefCell<TreeNode>>>,
274+
map: &mut HashMap<i32, i32>,
275+
max: &mut i32,
276+
) -> i32 {
277+
if root.is_none() {
278+
return 0;
279+
}
280+
let node = root.as_ref().unwrap().borrow();
281+
let sum = node.val + Self::dfs(&node.left, map, max) + Self::dfs(&node.right, map, max);
282+
map.insert(sum, map.get(&sum).unwrap_or(&0) + 1);
283+
*max = (*max).max(map[&sum]);
284+
sum
285+
}
286+
287+
pub fn find_frequent_tree_sum(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
288+
let mut map = HashMap::new();
289+
let mut max = 0;
290+
let mut res = Vec::new();
291+
Self::dfs(&root, &mut map, &mut max);
292+
for (k, v) in map.into_iter() {
293+
if v == max {
294+
res.push(k);
295+
}
296+
}
297+
res
298+
}
299+
}
300+
```
301+
206302
### **...**
207303

208304
```

solution/0500-0599/0508.Most Frequent Subtree Sum/README_EN.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,102 @@ func findFrequentTreeSum(root *TreeNode) []int {
187187
}
188188
```
189189

190+
### **TypeScript**
191+
192+
```ts
193+
/**
194+
* Definition for a binary tree node.
195+
* class TreeNode {
196+
* val: number
197+
* left: TreeNode | null
198+
* right: TreeNode | null
199+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
200+
* this.val = (val===undefined ? 0 : val)
201+
* this.left = (left===undefined ? null : left)
202+
* this.right = (right===undefined ? null : right)
203+
* }
204+
* }
205+
*/
206+
207+
function findFrequentTreeSum(root: TreeNode | null): number[] {
208+
const map = new Map<number, number>();
209+
let max = 0;
210+
const dfs = (root: TreeNode | null) => {
211+
if (root == null) {
212+
return 0;
213+
}
214+
const { val, left, right } = root;
215+
const sum = val + dfs(left) + dfs(right);
216+
map.set(sum, (map.get(sum) ?? 0) + 1);
217+
max = Math.max(max, map.get(sum));
218+
return sum;
219+
};
220+
dfs(root);
221+
const res = [];
222+
for (const [k, v] of map) {
223+
if (v === max) {
224+
res.push(k);
225+
}
226+
}
227+
return res;
228+
}
229+
```
230+
231+
### **Rust**
232+
233+
```rust
234+
// Definition for a binary tree node.
235+
// #[derive(Debug, PartialEq, Eq)]
236+
// pub struct TreeNode {
237+
// pub val: i32,
238+
// pub left: Option<Rc<RefCell<TreeNode>>>,
239+
// pub right: Option<Rc<RefCell<TreeNode>>>,
240+
// }
241+
//
242+
// impl TreeNode {
243+
// #[inline]
244+
// pub fn new(val: i32) -> Self {
245+
// TreeNode {
246+
// val,
247+
// left: None,
248+
// right: None
249+
// }
250+
// }
251+
// }
252+
use std::rc::Rc;
253+
use std::cell::RefCell;
254+
use std::collections::HashMap;
255+
impl Solution {
256+
fn dfs(
257+
root: &Option<Rc<RefCell<TreeNode>>>,
258+
map: &mut HashMap<i32, i32>,
259+
max: &mut i32,
260+
) -> i32 {
261+
if root.is_none() {
262+
return 0;
263+
}
264+
let node = root.as_ref().unwrap().borrow();
265+
let sum = node.val + Self::dfs(&node.left, map, max) + Self::dfs(&node.right, map, max);
266+
map.insert(sum, map.get(&sum).unwrap_or(&0) + 1);
267+
*max = (*max).max(map[&sum]);
268+
sum
269+
}
270+
271+
pub fn find_frequent_tree_sum(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
272+
let mut map = HashMap::new();
273+
let mut max = 0;
274+
let mut res = Vec::new();
275+
Self::dfs(&root, &mut map, &mut max);
276+
for (k, v) in map.into_iter() {
277+
if v == max {
278+
res.push(k);
279+
}
280+
}
281+
res
282+
}
283+
}
284+
```
285+
190286
### **...**
191287

192288
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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<i32, i32>,
26+
max: &mut i32,
27+
) -> i32 {
28+
if root.is_none() {
29+
return 0;
30+
}
31+
let node = root.as_ref().unwrap().borrow();
32+
let sum = node.val + Self::dfs(&node.left, map, max) + Self::dfs(&node.right, map, max);
33+
map.insert(sum, map.get(&sum).unwrap_or(&0) + 1);
34+
*max = (*max).max(map[&sum]);
35+
sum
36+
}
37+
38+
pub fn find_frequent_tree_sum(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
39+
let mut map = HashMap::new();
40+
let mut max = 0;
41+
let mut res = Vec::new();
42+
Self::dfs(&root, &mut map, &mut max);
43+
for (k, v) in map.into_iter() {
44+
if v == max {
45+
res.push(k);
46+
}
47+
}
48+
res
49+
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 findFrequentTreeSum(root: TreeNode | null): number[] {
16+
const map = new Map<number, number>();
17+
let max = 0;
18+
const dfs = (root: TreeNode | null) => {
19+
if (root == null) {
20+
return 0;
21+
}
22+
const { val, left, right } = root;
23+
const sum = val + dfs(left) + dfs(right);
24+
map.set(sum, (map.get(sum) ?? 0) + 1);
25+
max = Math.max(max, map.get(sum));
26+
return sum;
27+
};
28+
dfs(root);
29+
const res = [];
30+
for (const [k, v] of map) {
31+
if (v === max) {
32+
res.push(k);
33+
}
34+
}
35+
return res;
36+
}

0 commit comments

Comments
 (0)