diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5f69f53d..1743243d 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -319,6 +319,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 +} +``` + + **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** @@ -548,6 +578,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/) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 2229a854..65a155fb 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.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.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 224caa5e..b1331659 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.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 + } +``` + -----------------------