|
| 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