437. Path Sum III Medium

@problem@discussion
#Tree#Depth-First Search#Binary Tree



1/**
2 * [437] Path Sum III
3 *
4 * Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.
5 * The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg" style="width: 450px; height: 386px;" />
9 * Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
10 * Output: 3
11 * Explanation: The paths that sum to 8 are shown.
12 * 
13 * Example 2:
14 * 
15 * Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
16 * Output: 3
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	The number of nodes in the tree is in the range [0, 1000].
22 * 	-10^9 <= Node.val <= 10^9
23 * 	-1000 <= targetSum <= 1000
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/path-sum-iii/
30// discuss: https://leetcode.com/problems/path-sum-iii/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_437() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.