mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0513.找树左下角的值.md
This commit is contained in:
@ -621,6 +621,43 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
**递归**
|
||||
|
||||
```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"/>
|
||||
|
Reference in New Issue
Block a user