Merge pull request #1766 from Jack-Zhang-1314/patch-22

Update 0513.找树左下角的值.md about rust
This commit is contained in:
程序员Carl
2022-12-05 09:51:14 +08:00
committed by GitHub

View File

@ -580,6 +580,77 @@ object Solution {
}
```
### rust
**层序遍历**
```rust
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
impl Solution {
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut queue = VecDeque::new();
let mut res = 0;
if root.is_some() {
queue.push_back(root);
}
while !queue.is_empty() {
for i in 0..queue.len() {
let node = queue.pop_front().unwrap().unwrap();
if i == 0 {
res = node.borrow().val;
}
if node.borrow().left.is_some() {
queue.push_back(node.borrow().left.clone());
}
if node.borrow().right.is_some() {
queue.push_back(node.borrow().right.clone());
}
}
}
res
}
}
```
**递归**
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
//*递归*/
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut res = 0;
let mut max_depth = i32::MIN;
Self::traversal(root, 0, &mut max_depth, &mut res);
res
}
fn traversal(
root: Option<Rc<RefCell<TreeNode>>>,
depth: i32,
max_depth: &mut i32,
res: &mut i32,
) {
let node = root.unwrap();
if node.borrow().left.is_none() && node.borrow().right.is_none() {
if depth > *max_depth {
*max_depth = depth;
*res = node.borrow().val;
}
return;
}
if node.borrow().left.is_some() {
Self::traversal(node.borrow().left.clone(), depth + 1, max_depth, res);
}
if node.borrow().right.is_some() {
Self::traversal(node.borrow().right.clone(), depth + 1, max_depth, res);
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>