From 032527d25ad573ab3e7c3c715d2f6d3d66f0968f Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:43:25 +0800 Subject: [PATCH] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 70be7051..622a11b5 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -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>>) -> 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>>, + 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); + } + } +} +``` +