938. Range Sum of BST Easy

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



1/**
2 * [938] Range Sum of BST
3 *
4 * 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].
5 *  
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg" style="width: 400px; height: 222px;" />
8 * Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
9 * Output: 32
10 * Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg" style="width: 400px; height: 335px;" />
14 * Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
15 * Output: 23
16 * Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	The number of nodes in the tree is in the range [1, 2 * 10^4].
22 * 	1 <= Node.val <= 10^5
23 * 	1 <= low <= high <= 10^5
24 * 	All Node.val are unique.
25 * 
26 */
27pub struct Solution {}
28use crate::util::tree::{TreeNode, to_tree};
29
30// problem: https://leetcode.com/problems/range-sum-of-bst/
31// discuss: https://leetcode.com/problems/range-sum-of-bst/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for a binary tree node.
36// #[derive(Debug, PartialEq, Eq)]
37// pub struct TreeNode {
38//   pub val: i32,
39//   pub left: Option<Rc<RefCell<TreeNode>>>,
40//   pub right: Option<Rc<RefCell<TreeNode>>>,
41// }
42// 
43// impl TreeNode {
44//   #[inline]
45//   pub fn new(val: i32) -> Self {
46//     TreeNode {
47//       val,
48//       left: None,
49//       right: None
50//     }
51//   }
52// }
53use std::rc::Rc;
54use std::cell::RefCell;
55impl Solution {
56    pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_938() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.