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

[pull] master from youngyangyang04:master #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 18 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
abe0023
添加 0001.两数之和.md Scala版本
wzqwtt May 13, 2022
53379c0
添加 0454.四数相加II.md Scala版本
wzqwtt May 13, 2022
16c6abf
添加 0383.赎金信.md Scala版本
wzqwtt May 13, 2022
ca27111
添加(0714.买卖股票的最佳时机含手续费动态规划.md):增加typescript版本
xiaofei-2020 May 13, 2022
a53da7b
添加 0015.三数之和.md Scala版本
wzqwtt May 14, 2022
68eed4a
添加 0018.四数之和.md Scala版本
wzqwtt May 14, 2022
e4da60a
添加 0344.反转字符串.md Scala版本
wzqwtt May 14, 2022
f2dcdbe
添加 0541.反转字符串II.md Scala版本
wzqwtt May 14, 2022
037bebb
添加 剑指Offer05.替换空格.md Scala版本
wzqwtt May 14, 2022
1c369bb
102 in rust
3Xpl0it3r May 14, 2022
b8b62ff
树深度 rust实现
3Xpl0it3r May 18, 2022
9611896
Merge branch 'youngyangyang04:master' into master
3Xpl0it3r May 18, 2022
8c2737d
Merge branch 'master' into patch08
youngyangyang04 Jun 4, 2022
71a9111
Merge pull request #1326 from wzqwtt/patch08
youngyangyang04 Jun 4, 2022
b91d3c2
Merge pull request #1327 from xiaofei-2020/dp39
youngyangyang04 Jun 4, 2022
83726ac
Merge pull request #1328 from wzqwtt/patch09
youngyangyang04 Jun 4, 2022
dab44f5
Merge pull request #1329 from wzqwtt/patch10
youngyangyang04 Jun 4, 2022
9c32528
Merge pull request #1330 from 3Xpl0it3r/master
youngyangyang04 Jun 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
树深度 rust实现
  • Loading branch information
3Xpl0it3r committed May 18, 2022
commit b8b62ffc32de46005c4317150f952ad1aa483f5c
27 changes: 27 additions & 0 deletions problems/0104.二叉树的最大深度.md
View file Open in desktop
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
View file Open in desktop
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>

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