Skip to content

Commit

Permalink
Add problem 0235: Lowest Common Ancestor of a Binary Search Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed May 20, 2024
1 parent 8d92344 commit 7ce6947
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub mod problem_0230_kth_smallest_element_in_a_bst;
pub mod problem_0231_power_of_two;
pub mod problem_0232_implement_queue_using_stacks;
pub mod problem_0234_palindrome_linked_list;
pub mod problem_0235_lowest_common_ancestor_of_a_binary_search_tree;
pub mod problem_0240_search_a_2d_matrix_ii;
pub mod problem_0242_valid_anagram;
pub mod problem_0257_binary_tree_paths;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::data_structures::TreeNode;

pub struct Solution;

use std::cell::RefCell;
use std::rc::Rc;

impl Solution {
#[allow(clippy::unnecessary_wraps)]
pub fn lowest_common_ancestor(
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;
let left = p.min(q);
let right = p.max(q);
let mut root = root.unwrap();
loop {
root = if root.borrow().val > right {
root.borrow_mut().left.take().unwrap()
} else if root.borrow().val < left {
root.borrow_mut().right.take().unwrap()
} else {
break;
}
}
Some(root)
}
}

impl super::Solution for Solution {
fn lowest_common_ancestor(
root: Option<Rc<RefCell<TreeNode>>>,
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
Self::lowest_common_ancestor(root, p, q)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pub mod iterative;

use crate::data_structures::TreeNode;
use std::cell::RefCell;
use std::rc::Rc;

pub trait Solution {
fn lowest_common_ancestor(
root: Option<Rc<RefCell<TreeNode>>>,
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>>;
}

#[cfg(test)]
mod tests {
use super::Solution;
use crate::test_utilities;

pub fn run<S: Solution>() {
let test_cases = [
(
(
&[
Some(6),
Some(2),
Some(8),
Some(0),
Some(4),
Some(7),
Some(9),
None,
None,
Some(3),
Some(5),
] as &[_],
2,
8,
),
6,
),
(
(
&[
Some(6),
Some(2),
Some(8),
Some(0),
Some(4),
Some(7),
Some(9),
None,
None,
Some(3),
Some(5),
],
2,
4,
),
2,
),
((&[Some(2), Some(1)], 2, 1), 2),
(
(
&[
Some(6),
Some(2),
Some(8),
Some(0),
Some(4),
Some(7),
Some(9),
None,
None,
Some(3),
Some(5),
],
3,
5,
),
4,
),
];

for ((root, p, q), expected) in test_cases {
let root = test_utilities::make_tree(root.iter().copied());
let p = test_utilities::find_node(&root, p);
let q = test_utilities::find_node(&root, q);
let expected = test_utilities::find_node(&root, expected);

assert_eq!(S::lowest_common_ancestor(root, p, q), expected);
}
}
}

0 comments on commit 7ce6947

Please sign in to comment.