Skip to content

Commit 8199832

Browse files
committed
feat: add rust solution to lc problem: No.0814
No.0814.Binary Tree Pruning
1 parent 00e56b1 commit 8199832

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

solution/0800-0899/0814.Binary Tree Pruning/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:DFS**
54+
55+
观察叶节点,当叶节点 `val` 为 0 时,便将该节点抹去。回溯,查看其父节点是否成为了新的叶节点,依照此规则自底向上。
56+
5357
<!-- tabs:start -->
5458

5559
### **Python3**
@@ -215,6 +219,49 @@ function pruneTree(root: TreeNode | null): TreeNode | null {
215219
}
216220
```
217221

222+
### **Rust**
223+
224+
```rust
225+
// Definition for a binary tree node.
226+
// #[derive(Debug, PartialEq, Eq)]
227+
// pub struct TreeNode {
228+
// pub val: i32,
229+
// pub left: Option<Rc<RefCell<TreeNode>>>,
230+
// pub right: Option<Rc<RefCell<TreeNode>>>,
231+
// }
232+
//
233+
// impl TreeNode {
234+
// #[inline]
235+
// pub fn new(val: i32) -> Self {
236+
// TreeNode {
237+
// val,
238+
// left: None,
239+
// right: None
240+
// }
241+
// }
242+
// }
243+
use std::rc::Rc;
244+
use std::cell::RefCell;
245+
impl Solution {
246+
pub fn prune_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
247+
if root.is_none() {
248+
return None;
249+
}
250+
251+
let root = root.unwrap();
252+
let left = Self::prune_tree(root.borrow_mut().left.take());
253+
let right = Self::prune_tree(root.borrow_mut().right.take());
254+
if root.borrow().val == 0 && left.is_none() && right.is_none() {
255+
return None;
256+
}
257+
258+
root.borrow_mut().left = left;
259+
root.borrow_mut().right = right;
260+
Some(root)
261+
}
262+
}
263+
```
264+
218265
### **...**
219266

220267
```

solution/0800-0899/0814.Binary Tree Pruning/README_EN.md

+43
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,49 @@ function pruneTree(root: TreeNode | null): TreeNode | null {
204204
}
205205
```
206206

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

209252
```
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+
pub fn prune_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
23+
if root.is_none() {
24+
return None;
25+
}
26+
27+
let root = root.unwrap();
28+
let left = Self::prune_tree(root.borrow_mut().left.take());
29+
let right = Self::prune_tree(root.borrow_mut().right.take());
30+
if root.borrow().val == 0 && left.is_none() && right.is_none() {
31+
return None;
32+
}
33+
34+
root.borrow_mut().left = left;
35+
root.borrow_mut().right = right;
36+
Some(root)
37+
}
38+
}

0 commit comments

Comments
 (0)