From b27d1ff7397aca9c2ceb3156cc9f00045a958041 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Thu, 21 Nov 2024 21:48:32 +0800 Subject: [PATCH] Add problem 2331: Evaluate Boolean Binary Tree --- src/lib.rs | 1 + .../mod.rs | 135 ++++++++++++++++++ .../recursive.rs | 49 +++++++ 3 files changed, 185 insertions(+) create mode 100644 src/problem_2331_evaluate_boolean_binary_tree/mod.rs create mode 100644 src/problem_2331_evaluate_boolean_binary_tree/recursive.rs diff --git a/src/lib.rs b/src/lib.rs index fe7682e0..0b726e34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1729,6 +1729,7 @@ pub mod problem_2320_count_number_of_ways_to_place_houses; pub mod problem_2321_maximum_score_of_spliced_array; pub mod problem_2325_decode_the_message; pub mod problem_2326_spiral_matrix_iv; +pub mod problem_2331_evaluate_boolean_binary_tree; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2331_evaluate_boolean_binary_tree/mod.rs b/src/problem_2331_evaluate_boolean_binary_tree/mod.rs new file mode 100644 index 00000000..4a5974d5 --- /dev/null +++ b/src/problem_2331_evaluate_boolean_binary_tree/mod.rs @@ -0,0 +1,135 @@ +use crate::data_structures::TreeNode; +use std::cell::RefCell; +use std::rc::Rc; + +pub mod recursive; + +pub trait Solution { + fn evaluate_tree(root: Option>>) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + use crate::test_utilities; + + type TestCase<'a> = (&'a [Option], bool); + + const EXTRA_TEST_CASE_1: TestCase = ( + &[ + Some(3), + Some(3), + Some(2), + Some(0), + Some(0), + Some(3), + Some(2), + None, + None, + None, + None, + Some(1), + Some(3), + Some(1), + Some(1), + None, + None, + Some(2), + Some(1), + None, + None, + None, + None, + Some(1), + Some(1), + None, + None, + None, + None, + None, + None, + ], + false, + ); + + const EXTRA_TEST_CASE_2: TestCase = ( + &[ + Some(3), + Some(2), + Some(3), + Some(3), + Some(3), + Some(3), + Some(3), + Some(2), + Some(2), + Some(2), + Some(1), + Some(2), + Some(0), + Some(0), + Some(3), + Some(0), + Some(2), + Some(0), + Some(1), + Some(0), + Some(0), + None, + None, + Some(0), + Some(1), + None, + None, + None, + None, + Some(1), + Some(1), + None, + None, + Some(3), + Some(0), + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + Some(1), + Some(0), + None, + None, + None, + None, + None, + None, + ], + false, + ); + + pub fn run() { + let test_cases = [ + (&[Some(2), Some(1), Some(3), None, None, Some(0), Some(1)] as &[_], true), + (&[Some(0)], false), + EXTRA_TEST_CASE_1, + EXTRA_TEST_CASE_2, + ]; + + for (root, expected) in test_cases { + assert_eq!( + S::evaluate_tree(test_utilities::make_tree(root.iter().copied())), + expected, + ); + } + } +} diff --git a/src/problem_2331_evaluate_boolean_binary_tree/recursive.rs b/src/problem_2331_evaluate_boolean_binary_tree/recursive.rs new file mode 100644 index 00000000..9aee4560 --- /dev/null +++ b/src/problem_2331_evaluate_boolean_binary_tree/recursive.rs @@ -0,0 +1,49 @@ +use crate::data_structures::TreeNode; + +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::cell::RefCell; +use std::rc::Rc; + +impl Solution { + fn helper(node: &Option>>) -> bool { + let node = node.as_deref().unwrap().borrow(); + let node = &*node; + + match node.val { + 0 => false, + 1 => true, + operator => { + let lhs = Self::helper(&node.left); + + if operator == 2 { + lhs || Self::helper(&node.right) + } else { + lhs && Self::helper(&node.right) + } + } + } + } + + pub fn evaluate_tree(root: Option>>) -> bool { + Self::helper(&root) + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn evaluate_tree(root: Option>>) -> bool { + Self::evaluate_tree(root) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}