|
| 1 | +// https://leetcode.com/problems/range-sum-of-bst |
| 2 | +// |
| 3 | +// Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`. |
| 4 | +// |
| 5 | +// **Example 1:** |
| 6 | +// |
| 7 | +//  |
| 8 | +// ``` |
| 9 | +// **Input:** root = [10,5,15,3,7,null,18], low = 7, high = 15 |
| 10 | +// **Output:** 32 |
| 11 | +// **Explanation:** Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32. |
| 12 | +// ``` |
| 13 | +// |
| 14 | +// **Example 2:** |
| 15 | +// |
| 16 | +//  |
| 17 | +// ``` |
| 18 | +// **Input:** root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10 |
| 19 | +// **Output:** 23 |
| 20 | +// **Explanation:** Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23. |
| 21 | +// ``` |
| 22 | +// |
| 23 | +// **Constraints:** |
| 24 | +// |
| 25 | +// * The number of nodes in the tree is in the range `[1, 2 * 10<sup>4</sup>]`. |
| 26 | +// * `1 <= Node.val <= 10<sup>5</sup>` |
| 27 | +// * `1 <= low <= high <= 10<sup>5</sup>` |
| 28 | +// * All `Node.val` are **unique**. |
| 29 | + |
| 30 | +// Definition for a binary tree node. |
| 31 | +// #[derive(Debug, PartialEq, Eq)] |
| 32 | +// pub struct TreeNode { |
| 33 | +// pub val: i32, |
| 34 | +// pub left: Option<Rc<RefCell<TreeNode>>>, |
| 35 | +// pub right: Option<Rc<RefCell<TreeNode>>>, |
| 36 | +// } |
| 37 | +// |
| 38 | +// impl TreeNode { |
| 39 | +// #[inline] |
| 40 | +// pub fn new(val: i32) -> Self { |
| 41 | +// TreeNode { |
| 42 | +// val, |
| 43 | +// left: None, |
| 44 | +// right: None |
| 45 | +// } |
| 46 | +// } |
| 47 | +// } |
| 48 | +use crate::base_tree::TreeNode; |
| 49 | + |
| 50 | +use std::cell::RefCell; |
| 51 | +use std::rc::Rc; |
| 52 | + |
| 53 | +pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> i32 { |
| 54 | + if root.is_none() { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + let mut sum = 0; |
| 58 | + let val = root.as_ref().unwrap().borrow().val; |
| 59 | + if val >= low && val <= high { |
| 60 | + sum = sum + val; |
| 61 | + } |
| 62 | + sum = sum + range_sum_bst(root.as_ref().unwrap().borrow().left.clone(), low, high); |
| 63 | + sum = sum + range_sum_bst(root.as_ref().unwrap().borrow().right.clone(), low, high); |
| 64 | + return sum; |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +pub fn t1() { |
| 69 | + let mut root = TreeNode::new(10); |
| 70 | + root.left = Some(Rc::new(RefCell::new(TreeNode::new(5)))); |
| 71 | + root.right = Some(Rc::new(RefCell::new(TreeNode::new(15)))); |
| 72 | + root.left.as_ref().unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(3)))); |
| 73 | + root.left.as_ref().unwrap().borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(7)))); |
| 74 | + root.right.as_ref().unwrap().borrow_mut().right = |
| 75 | + Some(Rc::new(RefCell::new(TreeNode::new(18)))); |
| 76 | + assert_eq!(range_sum_bst(Some(Rc::new(RefCell::new(root))), 7, 15), 32); |
| 77 | +} |
0 commit comments