Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d610b11

Browse files
Merge pull request youngyangyang04#1755 from Jack-Zhang-1314/patch-16
Update 0111.二叉树的最小深度.md about rust
2 parents adab079 + 46e1d0b commit d610b11

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

‎problems/0111.二叉树的最小深度.md‎

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -574,66 +574,48 @@ object Solution {
574574
rust:
575575
```rust
576576
impl Solution {
577-
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
578-
return Solution::bfs(root)
579-
}
580-
581577
// 递归
582-
pub fn dfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
583-
if node.is_none(){
584-
return 0;
585-
}
586-
let parent = node.unwrap();
587-
let left_child = parent.borrow_mut().left.take();
588-
let right_child = parent.borrow_mut().right.take();
589-
if left_child.is_none() && right_child.is_none(){
590-
return 1;
591-
}
592-
let mut min_depth = i32::MAX;
593-
if left_child.is_some(){
594-
let left_depth = Solution::dfs(left_child);
595-
if left_depth <= min_depth{
596-
min_depth = left_depth
597-
}
598-
}
599-
if right_child.is_some(){
600-
let right_depth = Solution::dfs(right_child);
601-
if right_depth <= min_depth{
602-
min_depth = right_depth
578+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
579+
if let Some(node) = root {
580+
match (node.borrow().left.clone(), node.borrow().right.clone()) {
581+
(Some(n1), None) => 1 + Self::min_depth(Some(n1)),
582+
(None, Some(n2)) => 1 + Self::min_depth(Some(n2)),
583+
(Some(n1), Some(n2)) => {
584+
1 + std::cmp::min(Self::min_depth(Some(n1)), Self::min_depth(Some(n2)))
585+
}
586+
_ => 1,
603587
}
588+
} else {
589+
0
604590
}
605-
min_depth + 1
606-
607591
}
608592

609593
// 迭代
610-
pub fn bfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
611-
let mut min_depth = 0;
612-
if node.is_none(){
613-
return min_depth
594+
// 需要 use std::collections::VecDeque;
595+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
596+
let mut res = 0;
597+
let mut queue = VecDeque::new();
598+
if root.is_some() {
599+
queue.push_back(root);
614600
}
615-
let mut stack = vec![node.unwrap()];
616-
while !stack.is_empty(){
617-
min_depth += 1;
618-
let num = stack.len();
619-
for _i in 0..num{
620-
let top = stack.remove(0);
621-
let left_child = top.borrow_mut().left.take();
622-
let right_child = top.borrow_mut().right.take();
623-
if left_child.is_none() && right_child.is_none(){
624-
return min_depth;
601+
while !queue.is_empty() {
602+
res += 1;
603+
for _ in 0..queue.len() {
604+
let node = queue.pop_front().unwrap().unwrap();
605+
if node.borrow().left.is_none() && node.borrow().right.is_none() {
606+
return res;
625607
}
626-
if left_child.is_some(){
627-
stack.push(left_child.unwrap());
608+
if node.borrow().left.is_some(){
609+
queue.push_back(node.borrow().left.clone());
628610
}
629-
if right_child.is_some(){
630-
stack.push(right_child.unwrap());
611+
if node.borrow().right.is_some(){
612+
queue.push_back(node.borrow().right.clone());
631613
}
632614
}
633615
}
634-
min_depth
616+
res
635617
}
636-
618+
}
637619
```
638620

639621
<p align="center">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /