From 8b26d8e1931f39668c603ddb818e582f194e45e4 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:17:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 38 +++++++---------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 470e1919..301c84b1 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -574,36 +574,20 @@ object Solution { rust: ```rust impl Solution { - pub fn min_depth(root: Option>>) -> i32 { - return Solution::bfs(root) - } - // 递归 - pub fn dfs(node: Option>>) -> i32{ - if node.is_none(){ - return 0; - } - let parent = node.unwrap(); - let left_child = parent.borrow_mut().left.take(); - let right_child = parent.borrow_mut().right.take(); - if left_child.is_none() && right_child.is_none(){ - return 1; - } - let mut min_depth = i32::MAX; - if left_child.is_some(){ - let left_depth = Solution::dfs(left_child); - if left_depth <= min_depth{ - min_depth = left_depth + pub fn min_depth(root: Option>>) -> i32 { + if let Some(node) = root { + match (node.borrow().left.clone(), node.borrow().right.clone()) { + (Some(n1), None) => 1 + Self::min_depth(Some(n1)), + (None, Some(n2)) => 1 + Self::min_depth(Some(n2)), + (Some(n1), Some(n2)) => { + 1 + std::cmp::min(Self::min_depth(Some(n1)), Self::min_depth(Some(n2))) + } + _ => 1, } + } else { + 0 } - if right_child.is_some(){ - let right_depth = Solution::dfs(right_child); - if right_depth <= min_depth{ - min_depth = right_depth - } - } - min_depth + 1 - } // 迭代 From ad5520612d607547f0b4d7d8e9b97719ad9db9d4 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:22:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 37 +++++++++++------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 301c84b1..074a7165 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -591,33 +591,30 @@ impl Solution { } // 迭代 - pub fn bfs(node: Option>>) -> i32{ - let mut min_depth = 0; - if node.is_none(){ - return min_depth + pub fn min_depth(root: Option>>) -> i32 { + let mut res = 0; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); } - let mut stack = vec![node.unwrap()]; - while !stack.is_empty(){ - min_depth += 1; - let num = stack.len(); - for _i in 0..num{ - let top = stack.remove(0); - let left_child = top.borrow_mut().left.take(); - let right_child = top.borrow_mut().right.take(); - if left_child.is_none() && right_child.is_none(){ - return min_depth; + while !queue.is_empty() { + res += 1; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + if node.borrow().left.is_none() && node.borrow().right.is_none() { + return res; } - if left_child.is_some(){ - stack.push(left_child.unwrap()); + if node.borrow().left.is_some() { + queue.push_back(node.borrow().left.clone()); } - if right_child.is_some(){ - stack.push(right_child.unwrap()); + if node.borrow().right.is_some() { + queue.push_back(node.borrow().right.clone()); } } } - min_depth + res } - +} ```

From 46e1d0b907030592f6480b5cf04588b319bcab67 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:29:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 074a7165..0b2ec0f6 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -591,6 +591,7 @@ impl Solution { } // 迭代 + // 需要 use std::collections::VecDeque; pub fn min_depth(root: Option>>) -> i32 { let mut res = 0; let mut queue = VecDeque::new();