Skip to content

Commit 4932c6a

Browse files
committed
feat: add solutions to lcof problem: No.27
้ข่ฏ•้ข˜ 27. ไบŒๅ‰ๆ ‘็š„้•œๅƒ
1 parent 77ac804 commit 4932c6a

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

โ€Žlcof/้ข่ฏ•้ข˜27. ไบŒๅ‰ๆ ‘็š„้•œๅƒ/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,78 @@ public:
160160

161161
```
162162

163+
### **TypeScript**
164+
165+
```ts
166+
/**
167+
* Definition for a binary tree node.
168+
* class TreeNode {
169+
* val: number
170+
* left: TreeNode | null
171+
* right: TreeNode | null
172+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
173+
* this.val = (val===undefined ? 0 : val)
174+
* this.left = (left===undefined ? null : left)
175+
* this.right = (right===undefined ? null : right)
176+
* }
177+
* }
178+
*/
179+
180+
function mirrorTree(root: TreeNode | null): TreeNode | null {
181+
if (root == null) {
182+
return root;
183+
}
184+
const { left, right } = root;
185+
root.left = right;
186+
root.right = left;
187+
mirrorTree(left);
188+
mirrorTree(right);
189+
return root;
190+
}
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
// Definition for a binary tree node.
197+
// #[derive(Debug, PartialEq, Eq)]
198+
// pub struct TreeNode {
199+
// pub val: i32,
200+
// pub left: Option<Rc<RefCell<TreeNode>>>,
201+
// pub right: Option<Rc<RefCell<TreeNode>>>,
202+
// }
203+
//
204+
// impl TreeNode {
205+
// #[inline]
206+
// pub fn new(val: i32) -> Self {
207+
// TreeNode {
208+
// val,
209+
// left: None,
210+
// right: None
211+
// }
212+
// }
213+
// }
214+
use std::mem;
215+
use std::rc::Rc;
216+
use std::cell::RefCell;
217+
218+
impl Solution {
219+
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
220+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) {
221+
if let Some(node) = root {
222+
let mut node = node.borrow_mut();
223+
let lt = mem::replace(&mut node.left, None);
224+
let rt = mem::replace(&mut node.right, lt);
225+
mem::replace(&mut node.left, rt);
226+
dfs(&node.left);
227+
dfs(&node.right);
228+
}
229+
}
230+
dfs(&root);
231+
root
232+
}
233+
}
234+
```
163235
### **...**
164236

165237
```
Lines changed: 38 additions & 0 deletions
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::mem;
20+
use std::rc::Rc;
21+
use std::cell::RefCell;
22+
23+
impl Solution {
24+
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
25+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) {
26+
if let Some(node) = root {
27+
let mut node = node.borrow_mut();
28+
let lt = mem::replace(&mut node.left, None);
29+
let rt = mem::replace(&mut node.right, lt);
30+
mem::replace(&mut node.left, rt);
31+
dfs(&node.left);
32+
dfs(&node.right);
33+
}
34+
}
35+
dfs(&root);
36+
root
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function mirrorTree(root: TreeNode | null): TreeNode | null {
16+
if (root == null) {
17+
return root;
18+
}
19+
const { left, right } = root;
20+
root.left = right;
21+
root.right = left;
22+
mirrorTree(left);
23+
mirrorTree(right);
24+
return root;
25+
}

0 commit comments

Comments
ย (0)