-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
140 lines (120 loc) · 4.06 KB
/
lib.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-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,
}
}
}
pub struct Solution;
impl Solution {
// Time: O(tree_height), Memory: O(1)
pub fn lowest_common_ancestor(
mut root: Option<Rc<RefCell<TreeNode>>>,
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
let p = p.unwrap().borrow().val;
let q = q.unwrap().borrow().val;
loop {
if let Some(node) = root {
let val = node.borrow().val;
if p < val && q < val {
// p, q are on the left subtree
root = node.borrow().left.clone();
} else if val < p && val < q {
// p, q are on the right subtree
root = node.borrow().right.clone();
} else {
// p, q are either on different subtrees or at the node itself
return Some(node);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example1() {
let p = Some(Rc::new(RefCell::new(TreeNode {
val: 2,
left: Some(Rc::new(RefCell::new(TreeNode::new(0)))),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 4,
left: Some(Rc::new(RefCell::new(TreeNode::new(3)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(5)))),
}))),
})));
let q = Some(Rc::new(RefCell::new(TreeNode {
val: 8,
left: Some(Rc::new(RefCell::new(TreeNode::new(7)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(9)))),
})));
let root = Some(Rc::new(RefCell::new(TreeNode {
val: 6,
left: p.clone(),
right: q.clone(),
})));
assert_eq!(Solution::lowest_common_ancestor(root.clone(), p, q), root);
}
#[test]
fn example2() {
let q = Some(Rc::new(RefCell::new(TreeNode {
val: 4,
left: Some(Rc::new(RefCell::new(TreeNode::new(3)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(5)))),
})));
let p = Some(Rc::new(RefCell::new(TreeNode {
val: 2,
left: Some(Rc::new(RefCell::new(TreeNode::new(0)))),
right: q.clone(),
})));
let root = Some(Rc::new(RefCell::new(TreeNode {
val: 6,
left: p.clone(),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 8,
left: Some(Rc::new(RefCell::new(TreeNode::new(7)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(9)))),
}))),
})));
assert_eq!(Solution::lowest_common_ancestor(root, p.clone(), q), p);
}
#[test]
fn example3() {
let q = Some(Rc::new(RefCell::new(TreeNode {
val: 2,
left: Some(Rc::new(RefCell::new(TreeNode::new(0)))),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 4,
left: Some(Rc::new(RefCell::new(TreeNode::new(3)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(5)))),
}))),
})));
let p = Some(Rc::new(RefCell::new(TreeNode {
val: 6,
left: q.clone(),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 8,
left: Some(Rc::new(RefCell::new(TreeNode::new(7)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(9)))),
}))),
})));
let root = p.clone();
assert_eq!(Solution::lowest_common_ancestor(root.clone(), p, q), root);
}
}