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 9c32528

Browse files
Merge pull request youngyangyang04#1330 from 3Xpl0it3r/master
添加(102. 二叉树的层序遍历 I) Rust 版本
2 parents dab44f5 + 9611896 commit 9c32528

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

‎problems/0102.二叉树的层序遍历.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,36 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
319319
}
320320
```
321321

322+
Rust:
323+
324+
```rust
325+
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
326+
let mut ans = Vec::new();
327+
let mut stack = Vec::new();
328+
if root.is_none(){
329+
return ans;
330+
}
331+
stack.push(root.unwrap());
332+
while stack.is_empty()!= true{
333+
let num = stack.len();
334+
let mut level = Vec::new();
335+
for _i in 0..num{
336+
let tmp = stack.remove(0);
337+
level.push(tmp.borrow_mut().val);
338+
if tmp.borrow_mut().left.is_some(){
339+
stack.push(tmp.borrow_mut().left.take().unwrap());
340+
}
341+
if tmp.borrow_mut().right.is_some(){
342+
stack.push(tmp.borrow_mut().right.take().unwrap());
343+
}
344+
}
345+
ans.push(level);
346+
}
347+
ans
348+
}
349+
```
350+
351+
322352
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
323353

324354

@@ -548,6 +578,35 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
548578
}
549579
```
550580

581+
Rust:
582+
583+
```rust
584+
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
585+
let mut ans = Vec::new();
586+
let mut stack = Vec::new();
587+
if root.is_none(){
588+
return ans;
589+
}
590+
stack.push(root.unwrap());
591+
while stack.is_empty()!= true{
592+
let num = stack.len();
593+
let mut level = Vec::new();
594+
for _i in 0..num{
595+
let tmp = stack.remove(0);
596+
level.push(tmp.borrow_mut().val);
597+
if tmp.borrow_mut().left.is_some(){
598+
stack.push(tmp.borrow_mut().left.take().unwrap());
599+
}
600+
if tmp.borrow_mut().right.is_some(){
601+
stack.push(tmp.borrow_mut().right.take().unwrap());
602+
}
603+
}
604+
ans.push(level);
605+
}
606+
ans
607+
}
608+
```
609+
551610
# 199.二叉树的右视图
552611

553612
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)

‎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 によって変換されたページ (->オリジナル) /