Skip to content

Commit 4acd01a

Browse files
committed
feat: add rust solution to lc problem: No.0617
No.0617.Merge Two Binary Trees
1 parent a2a2d1c commit 4acd01a

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

solution/0600-0699/0617.Merge Two Binary Trees/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,51 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
182182
}
183183
```
184184

185+
### **Rust**
186+
187+
```rust
188+
// Definition for a binary tree node.
189+
// #[derive(Debug, PartialEq, Eq)]
190+
// pub struct TreeNode {
191+
// pub val: i32,
192+
// pub left: Option<Rc<RefCell<TreeNode>>>,
193+
// pub right: Option<Rc<RefCell<TreeNode>>>,
194+
// }
195+
//
196+
// impl TreeNode {
197+
// #[inline]
198+
// pub fn new(val: i32) -> Self {
199+
// TreeNode {
200+
// val,
201+
// left: None,
202+
// right: None
203+
// }
204+
// }
205+
// }
206+
use std::rc::Rc;
207+
use std::cell::RefCell;
208+
impl Solution {
209+
pub fn merge_trees(
210+
root1: Option<Rc<RefCell<TreeNode>>>,
211+
root2: Option<Rc<RefCell<TreeNode>>>,
212+
) -> Option<Rc<RefCell<TreeNode>>> {
213+
match (root1.is_some(), root2.is_some()) {
214+
(false, false) => None,
215+
(true, false) => root1,
216+
(false, true) => root2,
217+
(true, true) => {
218+
{
219+
let mut r1 = root1.as_ref().unwrap().borrow_mut();
220+
let mut r2 = root2.as_ref().unwrap().borrow_mut();
221+
r1.val += r2.val;
222+
r1.left = Self::merge_trees(r1.left.take(), r2.left.take());
223+
r1.right = Self::merge_trees(r1.right.take(), r2.right.take());
224+
}
225+
root1
226+
}
227+
}
228+
}
229+
}
230+
```
231+
185232
<!-- tabs:end -->

solution/0600-0699/0617.Merge Two Binary Trees/README_EN.md

+47
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,53 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
180180
}
181181
```
182182

183+
### **Rust**
184+
185+
```rust
186+
// Definition for a binary tree node.
187+
// #[derive(Debug, PartialEq, Eq)]
188+
// pub struct TreeNode {
189+
// pub val: i32,
190+
// pub left: Option<Rc<RefCell<TreeNode>>>,
191+
// pub right: Option<Rc<RefCell<TreeNode>>>,
192+
// }
193+
//
194+
// impl TreeNode {
195+
// #[inline]
196+
// pub fn new(val: i32) -> Self {
197+
// TreeNode {
198+
// val,
199+
// left: None,
200+
// right: None
201+
// }
202+
// }
203+
// }
204+
use std::rc::Rc;
205+
use std::cell::RefCell;
206+
impl Solution {
207+
pub fn merge_trees(
208+
root1: Option<Rc<RefCell<TreeNode>>>,
209+
root2: Option<Rc<RefCell<TreeNode>>>,
210+
) -> Option<Rc<RefCell<TreeNode>>> {
211+
match (root1.is_some(), root2.is_some()) {
212+
(false, false) => None,
213+
(true, false) => root1,
214+
(false, true) => root2,
215+
(true, true) => {
216+
{
217+
let mut r1 = root1.as_ref().unwrap().borrow_mut();
218+
let mut r2 = root2.as_ref().unwrap().borrow_mut();
219+
r1.val += r2.val;
220+
r1.left = Self::merge_trees(r1.left.take(), r2.left.take());
221+
r1.right = Self::merge_trees(r1.right.take(), r2.right.take());
222+
}
223+
root1
224+
}
225+
}
226+
}
227+
}
228+
```
229+
183230
### **...**
184231

185232
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 merge_trees(
23+
root1: Option<Rc<RefCell<TreeNode>>>,
24+
root2: Option<Rc<RefCell<TreeNode>>>,
25+
) -> Option<Rc<RefCell<TreeNode>>> {
26+
match (root1.is_some(), root2.is_some()) {
27+
(false, false) => None,
28+
(true, false) => root1,
29+
(false, true) => root2,
30+
(true, true) => {
31+
{
32+
let mut r1 = root1.as_ref().unwrap().borrow_mut();
33+
let mut r2 = root2.as_ref().unwrap().borrow_mut();
34+
r1.val += r2.val;
35+
r1.left = Self::merge_trees(r1.left.take(), r2.left.take());
36+
r1.right = Self::merge_trees(r1.right.take(), r2.right.take());
37+
}
38+
root1
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)