From 1c369bb83631f4449af7e646bc26bc86aacc5442 Mon Sep 17 00:00:00 2001 From: 3Xpl0it3r Date: Sat, 14 May 2022 23:24:57 +0800 Subject: [PATCH 1/2] 102 in rust --- ...02\345\272\217\351\201\215\345\216\206.md" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ab8f2e572e..1326781959 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -299,6 +299,36 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { } ``` +Rust: + +```rust +pub fn level_order(root: Option>>) -> Vec> { + 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 +} +``` + + **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** @@ -528,6 +558,35 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` +Rust: + +```rust +pub fn level_order(root: Option>>) -> Vec> { + 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/) From b8b62ffc32de46005c4317150f952ad1aa483f5c Mon Sep 17 00:00:00 2001 From: 3Xpl0it3r Date: Wed, 18 May 2022 18:31:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A0=91=E6=B7=B1=E5=BA=A6=20rust=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 27 ++++++++ ...00\345\260\217\346\267\261\345\272\246.md" | 64 +++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 2229a85434..65a155fbda 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -192,6 +192,33 @@ public: }; ``` +rust: +```rust +impl Solution { + pub fn max_depth(root: Option>>) -> 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叉树的最大深度 diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 224caa5e37..b13316591c 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int { } ``` +rust: +```rust +impl Solution { + pub fn min_depth(root: Option>>) -> i32 { + return Solution::bfs(root) + } + + // 递归 + pub fn dfs(node: Option>>) -> 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>>) -> 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 + } +``` + -----------------------