Skip to content

Commit f8246f3

Browse files
authored
feat: add rust solution to lc problem: No.0226 (doocs#1277)
1 parent 88a250f commit f8246f3

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

solution/0200-0299/0226.Invert Binary Tree/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,49 @@ public:
223223
};
224224
```
225225

226+
### **Rust**
227+
228+
```rust
229+
// Definition for a binary tree node.
230+
// #[derive(Debug, PartialEq, Eq)]
231+
// pub struct TreeNode {
232+
// pub val: i32,
233+
// pub left: Option<Rc<RefCell<TreeNode>>>,
234+
// pub right: Option<Rc<RefCell<TreeNode>>>,
235+
// }
236+
//
237+
// impl TreeNode {
238+
// #[inline]
239+
// pub fn new(val: i32) -> Self {
240+
// TreeNode {
241+
// val,
242+
// left: None,
243+
// right: None
244+
// }
245+
// }
246+
// }
247+
use std::rc::Rc;
248+
use std::cell::RefCell;
249+
impl Solution {
250+
#[allow(dead_code)]
251+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
252+
if root.is_none() {
253+
return root;
254+
}
255+
let left = root.as_ref().unwrap().borrow().left.clone();
256+
let right = root.as_ref().unwrap().borrow().right.clone();
257+
// Invert the subtree
258+
let inverted_left = Self::invert_tree(right);
259+
let inverted_right = Self::invert_tree(left);
260+
// Update the left & right
261+
root.as_ref().unwrap().borrow_mut().left = inverted_left;
262+
root.as_ref().unwrap().borrow_mut().right = inverted_right;
263+
// Return the root
264+
root
265+
}
266+
}
267+
```
268+
226269
### **Go**
227270

228271
```go

solution/0200-0299/0226.Invert Binary Tree/README_EN.md

+43
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,49 @@ public:
205205
};
206206
```
207207

208+
### **Rust**
209+
210+
```rust
211+
// Definition for a binary tree node.
212+
// #[derive(Debug, PartialEq, Eq)]
213+
// pub struct TreeNode {
214+
// pub val: i32,
215+
// pub left: Option<Rc<RefCell<TreeNode>>>,
216+
// pub right: Option<Rc<RefCell<TreeNode>>>,
217+
// }
218+
//
219+
// impl TreeNode {
220+
// #[inline]
221+
// pub fn new(val: i32) -> Self {
222+
// TreeNode {
223+
// val,
224+
// left: None,
225+
// right: None
226+
// }
227+
// }
228+
// }
229+
use std::rc::Rc;
230+
use std::cell::RefCell;
231+
impl Solution {
232+
#[allow(dead_code)]
233+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
234+
if root.is_none() {
235+
return root;
236+
}
237+
let left = root.as_ref().unwrap().borrow().left.clone();
238+
let right = root.as_ref().unwrap().borrow().right.clone();
239+
// Invert the subtree
240+
let inverted_left = Self::invert_tree(right);
241+
let inverted_right = Self::invert_tree(left);
242+
// Update the left & right
243+
root.as_ref().unwrap().borrow_mut().left = inverted_left;
244+
root.as_ref().unwrap().borrow_mut().right = inverted_right;
245+
// Return the root
246+
root
247+
}
248+
}
249+
```
250+
208251
### **Go**
209252

210253
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
24+
if root.is_none() {
25+
return root;
26+
}
27+
let left = root.as_ref().unwrap().borrow().left.clone();
28+
let right = root.as_ref().unwrap().borrow().right.clone();
29+
// Invert the subtree
30+
let inverted_left = Self::invert_tree(right);
31+
let inverted_right = Self::invert_tree(left);
32+
// Update the left & right
33+
root.as_ref().unwrap().borrow_mut().left = inverted_left;
34+
root.as_ref().unwrap().borrow_mut().right = inverted_right;
35+
// Return the root
36+
root
37+
}
38+
}

0 commit comments

Comments
 (0)