Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da9edfc

Browse files
authored
feat: add rust solution to lc problem: No.0095 (doocs#1405)
1 parent 15d8d08 commit da9edfc

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

‎solution/0000-0099/0095.Unique Binary Search Trees II/README.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,70 @@ public:
201201
};
202202
```
203203

204+
### **Rust**
205+
206+
```rust
207+
// Definition for a binary tree node.
208+
// #[derive(Debug, PartialEq, Eq)]
209+
// pub struct TreeNode {
210+
// pub val: i32,
211+
// pub left: Option<Rc<RefCell<TreeNode>>>,
212+
// pub right: Option<Rc<RefCell<TreeNode>>>,
213+
// }
214+
//
215+
// impl TreeNode {
216+
// #[inline]
217+
// pub fn new(val: i32) -> Self {
218+
// TreeNode {
219+
// val,
220+
// left: None,
221+
// right: None
222+
// }
223+
// }
224+
// }
225+
use std::rc::Rc;
226+
use std::cell::RefCell;
227+
impl Solution {
228+
#[allow(dead_code)]
229+
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
230+
Self::generate_trees_inner(1, n)
231+
}
232+
233+
#[allow(dead_code)]
234+
fn generate_trees_inner(left: i32, right: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
235+
let mut ret = Vec::new();
236+
237+
if left > right {
238+
// If there is no possible BST matching
239+
// Then this should be consider a nullptr
240+
ret.push(None);
241+
} else {
242+
// Otherwise, let's generate the BST
243+
for i in left..=right {
244+
// First get the two vectors containing the possible left trees & right trees
245+
let left_trees = Self::generate_trees_inner(left, i - 1);
246+
let right_trees = Self::generate_trees_inner(i + 1, right);
247+
248+
// Then construct the final results
249+
for left_tree in &left_trees {
250+
for right_tree in &right_trees {
251+
// Construct the current node
252+
let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i))));
253+
// Set the connection
254+
node.as_ref().unwrap().borrow_mut().left = left_tree.clone();
255+
node.as_ref().unwrap().borrow_mut().right = right_tree.clone();
256+
// Update the result vector
257+
ret.push(node);
258+
}
259+
}
260+
}
261+
}
262+
263+
ret
264+
}
265+
}
266+
```
267+
204268
### **Go**
205269

206270
```go

‎solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,70 @@ public:
187187
};
188188
```
189189

190+
### **Rust**
191+
192+
```rust
193+
// Definition for a binary tree node.
194+
// #[derive(Debug, PartialEq, Eq)]
195+
// pub struct TreeNode {
196+
// pub val: i32,
197+
// pub left: Option<Rc<RefCell<TreeNode>>>,
198+
// pub right: Option<Rc<RefCell<TreeNode>>>,
199+
// }
200+
//
201+
// impl TreeNode {
202+
// #[inline]
203+
// pub fn new(val: i32) -> Self {
204+
// TreeNode {
205+
// val,
206+
// left: None,
207+
// right: None
208+
// }
209+
// }
210+
// }
211+
use std::rc::Rc;
212+
use std::cell::RefCell;
213+
impl Solution {
214+
#[allow(dead_code)]
215+
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
216+
Self::generate_trees_inner(1, n)
217+
}
218+
219+
#[allow(dead_code)]
220+
fn generate_trees_inner(left: i32, right: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
221+
let mut ret = Vec::new();
222+
223+
if left > right {
224+
// If there is no possible BST matching
225+
// Then this should be consider a nullptr
226+
ret.push(None);
227+
} else {
228+
// Otherwise, let's generate the BST
229+
for i in left..=right {
230+
// First get the two vectors containing the possible left trees & right trees
231+
let left_trees = Self::generate_trees_inner(left, i - 1);
232+
let right_trees = Self::generate_trees_inner(i + 1, right);
233+
234+
// Then construct the final results
235+
for left_tree in &left_trees {
236+
for right_tree in &right_trees {
237+
// Construct the current node
238+
let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i))));
239+
// Set the connection
240+
node.as_ref().unwrap().borrow_mut().left = left_tree.clone();
241+
node.as_ref().unwrap().borrow_mut().right = right_tree.clone();
242+
// Update the result vector
243+
ret.push(node);
244+
}
245+
}
246+
}
247+
}
248+
249+
ret
250+
}
251+
}
252+
```
253+
190254
### **Go**
191255

192256
```go
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
#[allow(dead_code)]
23+
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
24+
Self::generate_trees_inner(1, n)
25+
}
26+
27+
#[allow(dead_code)]
28+
fn generate_trees_inner(left: i32, right: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
29+
let mut ret = Vec::new();
30+
31+
if left > right {
32+
// If there is no possible BST matching
33+
// Then this should be consider a nullptr
34+
ret.push(None);
35+
} else {
36+
// Otherwise, let's generate the BST
37+
for i in left..=right {
38+
// First get the two vectors containing the possible left trees & right trees
39+
let left_trees = Self::generate_trees_inner(left, i - 1);
40+
let right_trees = Self::generate_trees_inner(i + 1, right);
41+
42+
// Then construct the final results
43+
for left_tree in &left_trees {
44+
for right_tree in &right_trees {
45+
// Construct the current node
46+
let mut node = Some(Rc::new(RefCell::new(TreeNode::new(i))));
47+
// Set the connection
48+
node.as_ref().unwrap().borrow_mut().left = left_tree.clone();
49+
node.as_ref().unwrap().borrow_mut().right = right_tree.clone();
50+
// Update the result vector
51+
ret.push(node);
52+
}
53+
}
54+
}
55+
}
56+
57+
ret
58+
}
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /