-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path22-find-bottom-left-tree-value.rs
40 lines (39 loc) · 1.13 KB
/
22-find-bottom-left-tree-value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// https://leetcode.cn/problems/find-bottom-left-tree-value/
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut max_height = 0;
let mut ans = 0;
fn inorder(node: &Option<Rc<RefCell<TreeNode>>>, cur_h: i32, max_h: &mut i32, ans: &mut i32) {
if let Some(node) = node {
inorder(&node.borrow().left, cur_h + 1, max_h, ans);
if cur_h > *max_h {
*max_h = cur_h;
*ans = node.borrow().val;
}
inorder(&node.borrow().right, cur_h + 1, max_h, ans);
}
}
inorder(&root, 1, &mut max_height, &mut ans);
ans
}
}