Skip to content

Commit 789bb3a

Browse files
committed
feat: add solutions to lc problems: No.0113,0199,0450
- No.0113.Path Sum II - No.0199.Binary Tree Right Side View - No.0450.Delete Node in a BST
1 parent 570e360 commit 789bb3a

File tree

10 files changed

+673
-0
lines changed

10 files changed

+673
-0
lines changed

solution/0100-0199/0113.Path Sum II/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,65 @@ func pathSum(root *TreeNode, targetSum int) [][]int {
212212
}
213213
```
214214

215+
### **Rust**
216+
217+
```rust
218+
// Definition for a binary tree node.
219+
// #[derive(Debug, PartialEq, Eq)]
220+
// pub struct TreeNode {
221+
// pub val: i32,
222+
// pub left: Option<Rc<RefCell<TreeNode>>>,
223+
// pub right: Option<Rc<RefCell<TreeNode>>>,
224+
// }
225+
//
226+
// impl TreeNode {
227+
// #[inline]
228+
// pub fn new(val: i32) -> Self {
229+
// TreeNode {
230+
// val,
231+
// left: None,
232+
// right: None
233+
// }
234+
// }
235+
// }
236+
use std::rc::Rc;
237+
use std::cell::RefCell;
238+
impl Solution {
239+
fn dfs(
240+
root: Option<Rc<RefCell<TreeNode>>>,
241+
paths: &mut Vec<i32>,
242+
mut target_sum: i32,
243+
res: &mut Vec<Vec<i32>>,
244+
) {
245+
if let Some(node) = root {
246+
let mut node = node.borrow_mut();
247+
target_sum -= node.val;
248+
paths.push(node.val);
249+
if node.left.is_none() && node.right.is_none() {
250+
if target_sum == 0 {
251+
res.push(paths.clone());
252+
}
253+
} else {
254+
if node.left.is_some() {
255+
Self::dfs(node.left.take(), paths, target_sum, res);
256+
}
257+
if node.right.is_some() {
258+
Self::dfs(node.right.take(), paths, target_sum, res);
259+
}
260+
}
261+
paths.pop();
262+
}
263+
}
264+
265+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
266+
let mut res = vec![];
267+
let mut paths = vec![];
268+
Self::dfs(root, &mut paths, target_sum, &mut res);
269+
res
270+
}
271+
}
272+
```
273+
215274
### **...**
216275

217276
```

solution/0100-0199/0113.Path Sum II/README_EN.md

+59
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,65 @@ func pathSum(root *TreeNode, targetSum int) [][]int {
199199
}
200200
```
201201

202+
### **Rust**
203+
204+
```rust
205+
// Definition for a binary tree node.
206+
// #[derive(Debug, PartialEq, Eq)]
207+
// pub struct TreeNode {
208+
// pub val: i32,
209+
// pub left: Option<Rc<RefCell<TreeNode>>>,
210+
// pub right: Option<Rc<RefCell<TreeNode>>>,
211+
// }
212+
//
213+
// impl TreeNode {
214+
// #[inline]
215+
// pub fn new(val: i32) -> Self {
216+
// TreeNode {
217+
// val,
218+
// left: None,
219+
// right: None
220+
// }
221+
// }
222+
// }
223+
use std::rc::Rc;
224+
use std::cell::RefCell;
225+
impl Solution {
226+
fn dfs(
227+
root: Option<Rc<RefCell<TreeNode>>>,
228+
paths: &mut Vec<i32>,
229+
mut target_sum: i32,
230+
res: &mut Vec<Vec<i32>>,
231+
) {
232+
if let Some(node) = root {
233+
let mut node = node.borrow_mut();
234+
target_sum -= node.val;
235+
paths.push(node.val);
236+
if node.left.is_none() && node.right.is_none() {
237+
if target_sum == 0 {
238+
res.push(paths.clone());
239+
}
240+
} else {
241+
if node.left.is_some() {
242+
Self::dfs(node.left.take(), paths, target_sum, res);
243+
}
244+
if node.right.is_some() {
245+
Self::dfs(node.right.take(), paths, target_sum, res);
246+
}
247+
}
248+
paths.pop();
249+
}
250+
}
251+
252+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
253+
let mut res = vec![];
254+
let mut paths = vec![];
255+
Self::dfs(root, &mut paths, target_sum, &mut res);
256+
res
257+
}
258+
}
259+
```
260+
202261
### **...**
203262

204263
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
fn dfs(
23+
root: Option<Rc<RefCell<TreeNode>>>,
24+
paths: &mut Vec<i32>,
25+
mut target_sum: i32,
26+
res: &mut Vec<Vec<i32>>,
27+
) {
28+
if let Some(node) = root {
29+
let mut node = node.borrow_mut();
30+
target_sum -= node.val;
31+
paths.push(node.val);
32+
if node.left.is_none() && node.right.is_none() {
33+
if target_sum == 0 {
34+
res.push(paths.clone());
35+
}
36+
} else {
37+
if node.left.is_some() {
38+
Self::dfs(node.left.take(), paths, target_sum, res);
39+
}
40+
if node.right.is_some() {
41+
Self::dfs(node.right.take(), paths, target_sum, res);
42+
}
43+
}
44+
paths.pop();
45+
}
46+
}
47+
48+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
49+
let mut res = vec![];
50+
let mut paths = vec![];
51+
Self::dfs(root, &mut paths, target_sum, &mut res);
52+
res
53+
}
54+
}

solution/0100-0199/0199.Binary Tree Right Side View/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,58 @@ func rightSideView(root *TreeNode) []int {
193193
}
194194
```
195195

196+
### **Rust**
197+
198+
```rust
199+
// Definition for a binary tree node.
200+
// #[derive(Debug, PartialEq, Eq)]
201+
// pub struct TreeNode {
202+
// pub val: i32,
203+
// pub left: Option<Rc<RefCell<TreeNode>>>,
204+
// pub right: Option<Rc<RefCell<TreeNode>>>,
205+
// }
206+
//
207+
// impl TreeNode {
208+
// #[inline]
209+
// pub fn new(val: i32) -> Self {
210+
// TreeNode {
211+
// val,
212+
// left: None,
213+
// right: None
214+
// }
215+
// }
216+
// }
217+
use std::rc::Rc;
218+
use std::cell::RefCell;
219+
use std::collections::VecDeque;
220+
impl Solution {
221+
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
222+
let mut res = vec![];
223+
if root.is_none() {
224+
return res;
225+
}
226+
let mut q = VecDeque::new();
227+
q.push_back(root);
228+
while !q.is_empty() {
229+
let n = q.len();
230+
res.push(q[n - 1].as_ref().unwrap().borrow().val);
231+
for _ in 0..n {
232+
if let Some(node) = q.pop_front().unwrap() {
233+
let mut node = node.borrow_mut();
234+
if node.left.is_some() {
235+
q.push_back(node.left.take());
236+
}
237+
if node.right.is_some() {
238+
q.push_back(node.right.take());
239+
}
240+
}
241+
}
242+
}
243+
res
244+
}
245+
}
246+
```
247+
196248
### **...**
197249

198250
```

solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,58 @@ func rightSideView(root *TreeNode) []int {
179179
}
180180
```
181181

182+
### **Rust**
183+
184+
```rust
185+
// Definition for a binary tree node.
186+
// #[derive(Debug, PartialEq, Eq)]
187+
// pub struct TreeNode {
188+
// pub val: i32,
189+
// pub left: Option<Rc<RefCell<TreeNode>>>,
190+
// pub right: Option<Rc<RefCell<TreeNode>>>,
191+
// }
192+
//
193+
// impl TreeNode {
194+
// #[inline]
195+
// pub fn new(val: i32) -> Self {
196+
// TreeNode {
197+
// val,
198+
// left: None,
199+
// right: None
200+
// }
201+
// }
202+
// }
203+
use std::rc::Rc;
204+
use std::cell::RefCell;
205+
use std::collections::VecDeque;
206+
impl Solution {
207+
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
208+
let mut res = vec![];
209+
if root.is_none() {
210+
return res;
211+
}
212+
let mut q = VecDeque::new();
213+
q.push_back(root);
214+
while !q.is_empty() {
215+
let n = q.len();
216+
res.push(q[n - 1].as_ref().unwrap().borrow().val);
217+
for _ in 0..n {
218+
if let Some(node) = q.pop_front().unwrap() {
219+
let mut node = node.borrow_mut();
220+
if node.left.is_some() {
221+
q.push_back(node.left.take());
222+
}
223+
if node.right.is_some() {
224+
q.push_back(node.right.take());
225+
}
226+
}
227+
}
228+
}
229+
res
230+
}
231+
}
232+
```
233+
182234
### **...**
183235

184236
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
use std::collections::VecDeque;
22+
impl Solution {
23+
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
24+
let mut res = vec![];
25+
if root.is_none() {
26+
return res;
27+
}
28+
let mut q = VecDeque::new();
29+
q.push_back(root);
30+
while !q.is_empty() {
31+
let n = q.len();
32+
res.push(q[n - 1].as_ref().unwrap().borrow().val);
33+
for _ in 0..n {
34+
if let Some(node) = q.pop_front().unwrap() {
35+
let mut node = node.borrow_mut();
36+
if node.left.is_some() {
37+
q.push_back(node.left.take());
38+
}
39+
if node.right.is_some() {
40+
q.push_back(node.right.take());
41+
}
42+
}
43+
}
44+
}
45+
res
46+
}
47+
}

0 commit comments

Comments
 (0)