Update 0404.左叶子之和.md
This commit is contained in:
parent
c15b8a7a19
commit
15379361aa
|
|
@ -575,6 +575,31 @@ object Solution {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
**递归**
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
impl Solution {
|
||||||
|
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||||
|
let mut res = 0;
|
||||||
|
if let Some(node) = root {
|
||||||
|
if let Some(left) = &node.borrow().left {
|
||||||
|
if left.borrow().right.is_none() && left.borrow().right.is_none() {
|
||||||
|
res += left.borrow().val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res + Self::sum_of_left_leaves(node.borrow().left.clone())
|
||||||
|
+ Self::sum_of_left_leaves(node.borrow().right.clone())
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue