mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0513.找树左下角的值.md
This commit is contained in:
@ -587,6 +587,40 @@ 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 == 1 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<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"/>
|
||||||
|
Reference in New Issue
Block a user