-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrange-sum-of-bst.rs
69 lines (64 loc) · 1.87 KB
/
range-sum-of-bst.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 938. Range Sum of BST
// 🟢 Easy
//
// https://leetcode.com/problems/range-sum-of-bst/
//
// Tags: Tree - Depth-First Search - Binary Search Tree - Binary Tree
use std::cell::RefCell;
use std::rc::Rc;
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
#[allow(dead_code)]
struct Solution;
impl Solution {
/// Recursive solution, explore any child of the current BST node that could provide valid
/// values, for example, if root is already lower than the low boundary, we don't need to
/// explore its left branch because any value there will be lower.
///
/// Time complexity: O(n) - We could visit every node in the BST.
/// Space complexity: O(n) - The call stack will grow to the height of the tree which could be
/// the same as its size.
///
/// Runtime 10 ms Beats 94.74%
/// Memory 4.35 MB Beats 5.26%
#[allow(dead_code)]
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, low: i32, high: i32) -> i32 {
match root {
None => 0,
Some(node) => {
let node = node.borrow();
let val = node.val;
let mut res = 0;
if val >= low {
res += Self::range_sum_bst(node.left.clone(), low, high);
}
if val <= high {
res += Self::range_sum_bst(node.right.clone(), low, high);
if val >= low {
res += val;
}
}
res
}
}
}
}
// Tests.
fn main() {
todo!()
}