Update 0617.合并二叉树.md

This commit is contained in:
fw_qaq
2022-11-26 15:16:06 +08:00
committed by GitHub
parent 111016aef9
commit 3b3ed75666

View File

@ -691,6 +691,35 @@ object Solution {
}
```
### rust
递归:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn merge_trees(
root1: Option<Rc<RefCell<TreeNode>>>,
root2: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
if root1.is_none() {
return root2;
}
if root2.is_none() {
return root1;
}
let binding = root1.clone();
let mut node1 = binding.as_ref().unwrap().borrow_mut();
let node2 = root2.as_ref().unwrap().borrow_mut();
node1.left = Self::merge_trees(node1.left.clone(), node2.left.clone());
node1.right = Self::merge_trees(node1.right.clone(), node2.right.clone());
node1.val += node2.val;
root1
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">