diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/README.md b/solution/0000-0099/0095.Unique Binary Search Trees II/README.md index 096a182eafbe2..e6d9820093fa6 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/README.md +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/README.md @@ -201,6 +201,70 @@ public: }; ``` +### **Rust** + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn generate_trees(n: i32) -> Vec>>> { + Self::generate_trees_inner(1, n) + } + + #[allow(dead_code)] + fn generate_trees_inner(left: i32, right: i32) -> Vec>>> { + let mut ret = Vec::new(); + + if left > right { + // If there is no possible BST matching + // Then this should be consider a nullptr + ret.push(None); + } else { + // Otherwise, let's generate the BST + for i in left..=right { + // First get the two vectors containing the possible left trees & right trees + let left_trees = Self::generate_trees_inner(left, i - 1); + let right_trees = Self::generate_trees_inner(i + 1, right); + + // Then construct the final results + for left_tree in &left_trees { + for right_tree in &right_trees { + // Construct the current node + let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i)))); + // Set the connection + node.as_ref().unwrap().borrow_mut().left = left_tree.clone(); + node.as_ref().unwrap().borrow_mut().right = right_tree.clone(); + // Update the result vector + ret.push(node); + } + } + } + } + + ret + } +} +``` + ### **Go** ```go diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md b/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md index 7ef91c87933e9..ea6d885d3c2e7 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md @@ -187,6 +187,70 @@ public: }; ``` +### **Rust** + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn generate_trees(n: i32) -> Vec>>> { + Self::generate_trees_inner(1, n) + } + + #[allow(dead_code)] + fn generate_trees_inner(left: i32, right: i32) -> Vec>>> { + let mut ret = Vec::new(); + + if left > right { + // If there is no possible BST matching + // Then this should be consider a nullptr + ret.push(None); + } else { + // Otherwise, let's generate the BST + for i in left..=right { + // First get the two vectors containing the possible left trees & right trees + let left_trees = Self::generate_trees_inner(left, i - 1); + let right_trees = Self::generate_trees_inner(i + 1, right); + + // Then construct the final results + for left_tree in &left_trees { + for right_tree in &right_trees { + // Construct the current node + let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i)))); + // Set the connection + node.as_ref().unwrap().borrow_mut().left = left_tree.clone(); + node.as_ref().unwrap().borrow_mut().right = right_tree.clone(); + // Update the result vector + ret.push(node); + } + } + } + } + + ret + } +} +``` + ### **Go** ```go diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs new file mode 100644 index 0000000000000..c3bdef0ae9e91 --- /dev/null +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs @@ -0,0 +1,59 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + #[allow(dead_code)] + pub fn generate_trees(n: i32) -> Vec>>> { + Self::generate_trees_inner(1, n) + } + + #[allow(dead_code)] + fn generate_trees_inner(left: i32, right: i32) -> Vec>>> { + let mut ret = Vec::new(); + + if left > right { + // If there is no possible BST matching + // Then this should be consider a nullptr + ret.push(None); + } else { + // Otherwise, let's generate the BST + for i in left..=right { + // First get the two vectors containing the possible left trees & right trees + let left_trees = Self::generate_trees_inner(left, i - 1); + let right_trees = Self::generate_trees_inner(i + 1, right); + + // Then construct the final results + for left_tree in &left_trees { + for right_tree in &right_trees { + // Construct the current node + let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i)))); + // Set the connection + node.as_ref().unwrap().borrow_mut().left = left_tree.clone(); + node.as_ref().unwrap().borrow_mut().right = right_tree.clone(); + // Update the result vector + ret.push(node); + } + } + } + } + + ret + } +} \ No newline at end of file