Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-BinaryTree

This commit is contained in:
Yuhao Ju
2022-12-01 21:51:40 +08:00
committed by GitHub
4 changed files with 119 additions and 9 deletions

View File

@ -196,21 +196,26 @@ class Solution {
Python Python
```python3
``` python 3
class Solution: class Solution:
def removeElement(self, nums: List[int], val: int) -> int: def removeElement(self, nums: List[int], val: int) -> int:
# 快指针遍历元素 # 快指针
fast = 0 fast = 0 # 快指针
# 慢指针记录位置 slow = 0 # 慢指针
slow = 0 size = len(nums)
for fast in range(len(nums)): while fast < size: # 不加等于是因为a = size 时nums[a] 会越界
# slow 用来收集不等于 val 的值,如果 fast 对应值不等于 val则把它与 slow 替换
if nums[fast] != val: if nums[fast] != val:
nums[slow] = nums[fast] nums[slow] = nums[fast]
slow += 1 slow += 1
fast += 1
return slow return slow
``` ```
Go Go
```go ```go
func removeElement(nums []int, val int) int { func removeElement(nums []int, val int) int {

View File

@ -805,6 +805,38 @@ func getHeight(_ root: TreeNode?) -> Int {
} }
``` ```
### rust
递归
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
Self::get_depth(root) != -1
}
pub fn get_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
let right = Self::get_depth(root.as_ref().unwrap().borrow().left.clone());
let left = Self::get_depth(root.unwrap().borrow().right.clone());
if right == -1 {
return -1;
}
if left == -1 {
return -1;
}
if (right - left).abs() > 1 {
return -1;
}
1 + right.max(left)
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -793,6 +793,52 @@ object Solution {
} }
``` ```
rust:
// 递归
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
1 + Self::count_nodes(Rc::clone(root.as_ref().unwrap()).borrow().left.clone())
+ Self::count_nodes(root.unwrap().borrow().right.clone())
}
}
```
// 迭代
```rust
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::VecDeque;
impl Solution {
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut res = 0;
let mut queue = VecDeque::new();
if root.is_some() {
queue.push_back(root);
}
while !queue.is_empty() {
for _ in 0..queue.len() {
let node = queue.pop_front().unwrap().unwrap();
if node.borrow().left.is_some() {
queue.push_back(node.borrow().left.clone());
}
if node.borrow().right.is_some() {
queue.push_back(node.borrow().right.clone());
}
res += 1;
}
}
res
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -795,6 +795,33 @@ object Solution {
} }
``` ```
rust:
```rust
// 递归
impl Solution {
pub fn binary_tree_paths(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
let mut res = vec![];
Self::recur(&root, String::from(""), &mut res);
res
}
pub fn recur(node: &Option<Rc<RefCell<TreeNode>>>, mut path: String, res: &mut Vec<String>) {
let r = node.as_ref().unwrap().borrow();
path += format!("{}", r.val).as_str();
if r.left.is_none() && r.right.is_none() {
res.push(path.to_string());
return;
}
if r.left.is_some() {
Self::recur(&r.left, path.clone() + "->", res);
}
if r.right.is_some() {
Self::recur(&r.right, path + "->", res);
}
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>