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 b8b62ff

Browse files
树深度 rust实现
1 parent 1c369bb commit b8b62ff

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

‎problems/0104.二叉树的最大深度.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,33 @@ public:
192192
};
193193
```
194194

195+
rust:
196+
```rust
197+
impl Solution {
198+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
199+
if root.is_none(){
200+
return 0;
201+
}
202+
let mut max_depth: i32 = 0;
203+
let mut stack = vec![root.unwrap()];
204+
while !stack.is_empty() {
205+
let num = stack.len();
206+
for _i in 0..num{
207+
let top = stack.remove(0);
208+
if top.borrow_mut().left.is_some(){
209+
stack.push(top.borrow_mut().left.take().unwrap());
210+
}
211+
if top.borrow_mut().right.is_some(){
212+
stack.push(top.borrow_mut().right.take().unwrap());
213+
}
214+
}
215+
max_depth+=1;
216+
}
217+
max_depth
218+
}
219+
```
220+
221+
195222
那么我们可以顺便解决一下n叉树的最大深度问题
196223

197224
# 559.n叉树的最大深度

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int {
488488
}
489489
```
490490

491+
rust:
492+
```rust
493+
impl Solution {
494+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
495+
return Solution::bfs(root)
496+
}
497+
498+
// 递归
499+
pub fn dfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
500+
if node.is_none(){
501+
return 0;
502+
}
503+
let parent = node.unwrap();
504+
let left_child = parent.borrow_mut().left.take();
505+
let right_child = parent.borrow_mut().right.take();
506+
if left_child.is_none() && right_child.is_none(){
507+
return 1;
508+
}
509+
let mut min_depth = i32::MAX;
510+
if left_child.is_some(){
511+
let left_depth = Solution::dfs(left_child);
512+
if left_depth <= min_depth{
513+
min_depth = left_depth
514+
}
515+
}
516+
if right_child.is_some(){
517+
let right_depth = Solution::dfs(right_child);
518+
if right_depth <= min_depth{
519+
min_depth = right_depth
520+
}
521+
}
522+
min_depth + 1
523+
524+
}
525+
526+
// 迭代
527+
pub fn bfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
528+
let mut min_depth = 0;
529+
if node.is_none(){
530+
return min_depth
531+
}
532+
let mut stack = vec![node.unwrap()];
533+
while !stack.is_empty(){
534+
min_depth += 1;
535+
let num = stack.len();
536+
for _i in 0..num{
537+
let top = stack.remove(0);
538+
let left_child = top.borrow_mut().left.take();
539+
let right_child = top.borrow_mut().right.take();
540+
if left_child.is_none() && right_child.is_none(){
541+
return min_depth;
542+
}
543+
if left_child.is_some(){
544+
stack.push(left_child.unwrap());
545+
}
546+
if right_child.is_some(){
547+
stack.push(right_child.unwrap());
548+
}
549+
}
550+
}
551+
min_depth
552+
}
553+
```
554+
491555
-----------------------
492556
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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