Skip to content

Commit a6a1bff

Browse files
committed
'Path Sum II' soln
1 parent 55cccbc commit a6a1bff

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ Note: Some solutions have multiple approaches implemented
118118
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](./leetcode/balanced_binary_tree.rs) | Easy |
119119
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
120120
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](./leetcode/path_sum.rs) | Easy |
121-
| >113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122-
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
121+
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122+
| >114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
123123
| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](./leetcode/distinct_subsequences.rs) | Hard |
124124
| 116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) | [Solution](./leetcode/populating_next_right_pointers.rs) | Medium |
125125
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [Solution](./leetcode/populating_next_right_pointers_ii.rs) | Medium |

leetcode/path_sum_ii.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
///
2+
/// Problem: Path Sum II
3+
///
4+
/// Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths
5+
/// where the sum of the node values in the path equals targetSum. Each path should be
6+
/// returned as a list of the node values, not node references.
7+
///
8+
/// A root-to-leaf path is a path starting from the root and ending at any leaf node.
9+
/// A leaf is a node with no children.
10+
///
11+
/// Example 1:
12+
/// Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
13+
/// Output: [[5,4,11,2],[5,8,4,5]]
14+
/// Explanation: There are two paths whose sum equals targetSum:
15+
/// 5 + 4 + 11 + 2 = 22
16+
/// 5 + 8 + 4 + 5 = 22
17+
///
18+
/// Example 2:
19+
/// Input: root = [1,2,3], targetSum = 5
20+
/// Output: []
21+
///
22+
/// Example 3:
23+
/// Input: root = [1,2], targetSum = 0
24+
/// Output: []
25+
///
26+
/// Constraints:
27+
/// The number of nodes in the tree is in the range [0, 5000].
28+
/// -1000 <= Node.val <= 1000
29+
/// -1000 <= targetSum <= 1000
30+
///
31+
32+
// Definition for a binary tree node (provided by LeetCode)
33+
// #[derive(Debug, PartialEq, Eq)]
34+
// pub struct TreeNode {
35+
// pub val: i32,
36+
// pub left: Option<Rc<RefCell<TreeNode>>>,
37+
// pub right: Option<Rc<RefCell<TreeNode>>>,
38+
// }
39+
//
40+
// impl TreeNode {
41+
// #[inline]
42+
// pub fn new(val: i32) -> Self {
43+
// TreeNode {
44+
// val,
45+
// left: None,
46+
// right: None
47+
// }
48+
// }
49+
// }
50+
51+
// # Solution
52+
// Time complexity: O(n²)
53+
// Space complexity: O(h) - Where h is the height of the tree
54+
55+
use std::rc::Rc;
56+
use std::cell::RefCell;
57+
impl Solution {
58+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
59+
let mut result = Vec::new();
60+
let mut current_path = Vec::new();
61+
62+
Self::dfs(root, target_sum, &mut current_path, &mut result);
63+
64+
result
65+
}
66+
67+
fn dfs(
68+
node: Option<Rc<RefCell<TreeNode>>>,
69+
remaining: i32,
70+
current_path: &mut Vec<i32>,
71+
result: &mut Vec<Vec<i32>>
72+
) {
73+
if let Some(n) = node {
74+
let n_ref = n.borrow();
75+
let val = n_ref.val;
76+
77+
78+
current_path.push(val);
79+
80+
81+
if n_ref.left.is_none() && n_ref.right.is_none() && val == remaining {
82+
result.push(current_path.clone());
83+
}
84+
85+
86+
Self::dfs(n_ref.left.clone(), remaining - val, current_path, result);
87+
Self::dfs(n_ref.right.clone(), remaining - val, current_path, result);
88+
89+
90+
current_path.pop();
91+
}
92+
}
93+
}
94+

0 commit comments

Comments
 (0)