Update 0112.路径总和.md

This commit is contained in:
fw_qaq 2022-11-20 18:02:32 +08:00 committed by GitHub
parent c15b8a7a19
commit d3e35b3ae9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -1213,6 +1213,28 @@ object Solution {
}
```
## rust
### 112.路径总和.md
```rust
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
if root.is_none() {
return false;
}
let node = root.unwrap();
if node.borrow().left.is_none() && node.borrow().right.is_none() {
return node.borrow().val == target_sum;
}
return Self::has_path_sum(node.borrow().left.clone(), target_sum - node.borrow().val)
|| Self::has_path_sum(node.borrow().right.clone(), target_sum - node.borrow().val);
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>