mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1330 from 3Xpl0it3r/master
添加(102. 二叉树的层序遍历 I) Rust 版本
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
|
||||
|
||||
|
||||
@ -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/)
|
||||
|
@ -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叉树的最大深度
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user