-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_sum_3.rs
49 lines (43 loc) · 1.38 KB
/
path_sum_3.rs
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
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode { val, left: None, right: None }
}
}
use std::cell::RefCell;
use std::rc::Rc;
// 437. Path Sum III, Medium
// https://leetcode.com/problems/path-sum-iii/
impl Solution {
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> i32 {
fn bfs(node: Option<Rc<RefCell<TreeNode>>>, sums: Vec<i32>, target_sum: i32, res: &mut i32) {
if let Some(node) = node {
let node = node.borrow_mut();
let mut new_sums = Vec::new();
new_sums.push(node.val);
for sum in sums {
new_sums.push(sum + node.val);
if sum + node.val == target_sum {
*res += 1;
}
}
if node.val == target_sum {
*res += 1;
}
bfs(node.left.clone(), new_sums.clone(), target_sum, res);
bfs(node.right.clone(), new_sums.clone(), target_sum, res);
}
}
let mut res = 0;
bfs(root, vec![], target_sum, &mut res);
res
}
}
struct Solution {}