Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1330 from 3Xpl0it3r/master
Browse files Browse the repository at this point in the history
添加(102. 二叉树的层序遍历 I) Rust 版本
  • Loading branch information
youngyangyang04 committed Jun 4, 2022
2 parents dab44f5 + 9611896 commit 9c32528
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
59 changes: 59 additions & 0 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,36 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
}
```

Rust:

```rust
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut ans = Vec::new();
let mut stack = Vec::new();
if root.is_none(){
return ans;
}
stack.push(root.unwrap());
while stack.is_empty()!= true{
let num = stack.len();
let mut level = Vec::new();
for _i in 0..num{
let tmp = stack.remove(0);
level.push(tmp.borrow_mut().val);
if tmp.borrow_mut().left.is_some(){
stack.push(tmp.borrow_mut().left.take().unwrap());
}
if tmp.borrow_mut().right.is_some(){
stack.push(tmp.borrow_mut().right.take().unwrap());
}
}
ans.push(level);
}
ans
}
```


**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**


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

Rust:

```rust
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut ans = Vec::new();
let mut stack = Vec::new();
if root.is_none(){
return ans;
}
stack.push(root.unwrap());
while stack.is_empty()!= true{
let num = stack.len();
let mut level = Vec::new();
for _i in 0..num{
let tmp = stack.remove(0);
level.push(tmp.borrow_mut().val);
if tmp.borrow_mut().left.is_some(){
stack.push(tmp.borrow_mut().left.take().unwrap());
}
if tmp.borrow_mut().right.is_some(){
stack.push(tmp.borrow_mut().right.take().unwrap());
}
}
ans.push(level);
}
ans
}
```

# 199.二叉树的右视图

[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
Expand Down
27 changes: 27 additions & 0 deletions problems/0104.二叉树的最大深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ public:
};
```

rust:
```rust
impl Solution {
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none(){
return 0;
}
let mut max_depth: i32 = 0;
let mut stack = vec![root.unwrap()];
while !stack.is_empty() {
let num = stack.len();
for _i in 0..num{
let top = stack.remove(0);
if top.borrow_mut().left.is_some(){
stack.push(top.borrow_mut().left.take().unwrap());
}
if top.borrow_mut().right.is_some(){
stack.push(top.borrow_mut().right.take().unwrap());
}
}
max_depth+=1;
}
max_depth
}
```


那么我们可以顺便解决一下n叉树的最大深度问题

# 559.n叉树的最大深度
Expand Down
64 changes: 64 additions & 0 deletions problems/0111.二叉树的最小深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```

rust:
```rust
impl Solution {
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
return Solution::bfs(root)
}

// 递归
pub fn dfs(node: Option<Rc<RefCell<TreeNode>>>) -> 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
}
}
if right_child.is_some(){
let right_depth = Solution::dfs(right_child);
if right_depth <= min_depth{
min_depth = right_depth
}
}
min_depth + 1

}

// 迭代
pub fn bfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
let mut min_depth = 0;
if node.is_none(){
return min_depth
}
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;
}
if left_child.is_some(){
stack.push(left_child.unwrap());
}
if right_child.is_some(){
stack.push(right_child.unwrap());
}
}
}
min_depth
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 9c32528

Please sign in to comment.