mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-BinaryTree
This commit is contained in:
@ -1219,6 +1219,111 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
|
||||
### 112.路径总和.md
|
||||
|
||||
递归:
|
||||
|
||||
```rust
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
|
||||
if root.is_none() {
|
||||
return false;
|
||||
}
|
||||
let node = root.unwrap();
|
||||
if node.borrow().left.is_none() && node.borrow().right.is_none() {
|
||||
return node.borrow().val == target_sum;
|
||||
}
|
||||
return Self::has_path_sum(node.borrow().left.clone(), target_sum - node.borrow().val)
|
||||
|| Self::has_path_sum(node.borrow().right.clone(), target_sum - node.borrow().val);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
迭代:
|
||||
|
||||
```rust
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
|
||||
let mut stack = vec![];
|
||||
if let Some(node) = root {
|
||||
stack.push((node.borrow().val, node.to_owned()));
|
||||
}
|
||||
while !stack.is_empty() {
|
||||
let (value, node) = stack.pop().unwrap();
|
||||
if node.borrow().left.is_none() && node.borrow().right.is_none() && value == target_sum
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if node.borrow().left.is_some() {
|
||||
if let Some(r) = node.borrow().left.as_ref() {
|
||||
stack.push((r.borrow().val + value, r.to_owned()));
|
||||
}
|
||||
}
|
||||
if node.borrow().right.is_some() {
|
||||
if let Some(r) = node.borrow().right.as_ref() {
|
||||
stack.push((r.borrow().val + value, r.to_owned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
```
|
||||
|
||||
### 113.路径总和-ii
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
|
||||
let mut res = vec![];
|
||||
let mut route = vec![];
|
||||
if root.is_none() {
|
||||
return res;
|
||||
} else {
|
||||
route.push(root.as_ref().unwrap().borrow().val);
|
||||
}
|
||||
Self::recur(
|
||||
&root,
|
||||
target_sum - root.as_ref().unwrap().borrow().val,
|
||||
&mut res,
|
||||
&mut route,
|
||||
);
|
||||
res
|
||||
}
|
||||
|
||||
pub fn recur(
|
||||
root: &Option<Rc<RefCell<TreeNode>>>,
|
||||
sum: i32,
|
||||
res: &mut Vec<Vec<i32>>,
|
||||
route: &mut Vec<i32>,
|
||||
) {
|
||||
let node = root.as_ref().unwrap().borrow();
|
||||
if node.left.is_none() && node.right.is_none() && sum == 0 {
|
||||
res.push(route.to_vec());
|
||||
return;
|
||||
}
|
||||
if node.left.is_some() {
|
||||
let left = node.left.as_ref().unwrap();
|
||||
route.push(left.borrow().val);
|
||||
Self::recur(&node.left, sum - left.borrow().val, res, route);
|
||||
route.pop();
|
||||
}
|
||||
if node.right.is_some() {
|
||||
let right = node.right.as_ref().unwrap();
|
||||
route.push(right.borrow().val);
|
||||
Self::recur(&node.right, sum - right.borrow().val, res, route);
|
||||
route.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -576,6 +576,58 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
**递归**
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
let mut res = 0;
|
||||
if let Some(node) = root {
|
||||
if let Some(left) = &node.borrow().left {
|
||||
if left.borrow().right.is_none() && left.borrow().right.is_none() {
|
||||
res += left.borrow().val;
|
||||
}
|
||||
}
|
||||
res + Self::sum_of_left_leaves(node.borrow().left.clone())
|
||||
+ Self::sum_of_left_leaves(node.borrow().right.clone())
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**迭代:**
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
let mut res = 0;
|
||||
let mut stack = vec![root];
|
||||
while !stack.is_empty() {
|
||||
if let Some(node) = stack.pop().unwrap() {
|
||||
if let Some(left) = &node.borrow().left {
|
||||
if left.borrow().left.is_none() && left.borrow().right.is_none() {
|
||||
res += left.borrow().val;
|
||||
}
|
||||
stack.push(Some(left.to_owned()));
|
||||
}
|
||||
if let Some(right) = &node.borrow().right {
|
||||
stack.push(Some(right.to_owned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -580,6 +580,77 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### rust
|
||||
|
||||
**层序遍历**
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
let mut queue = VecDeque::new();
|
||||
let mut res = 0;
|
||||
if root.is_some() {
|
||||
queue.push_back(root);
|
||||
}
|
||||
while !queue.is_empty() {
|
||||
for i in 0..queue.len() {
|
||||
let node = queue.pop_front().unwrap().unwrap();
|
||||
if i == 0 {
|
||||
res = node.borrow().val;
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**递归**
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
//*递归*/
|
||||
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
let mut res = 0;
|
||||
let mut max_depth = i32::MIN;
|
||||
Self::traversal(root, 0, &mut max_depth, &mut res);
|
||||
res
|
||||
}
|
||||
fn traversal(
|
||||
root: Option<Rc<RefCell<TreeNode>>>,
|
||||
depth: i32,
|
||||
max_depth: &mut i32,
|
||||
res: &mut i32,
|
||||
) {
|
||||
let node = root.unwrap();
|
||||
if node.borrow().left.is_none() && node.borrow().right.is_none() {
|
||||
if depth > *max_depth {
|
||||
*max_depth = depth;
|
||||
*res = node.borrow().val;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if node.borrow().left.is_some() {
|
||||
Self::traversal(node.borrow().left.clone(), depth + 1, max_depth, res);
|
||||
}
|
||||
if node.borrow().right.is_some() {
|
||||
Self::traversal(node.borrow().right.clone(), depth + 1, max_depth, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -197,7 +197,6 @@ class Solution:
|
||||
if not visited[i][j] and grid[i][j] == 1:
|
||||
# 每一个新岛屿
|
||||
self.count = 0
|
||||
print(f'{self.count}')
|
||||
self.bfs(grid, visited, i, j)
|
||||
result = max(result, self.count)
|
||||
|
||||
|
@ -106,14 +106,11 @@ public:
|
||||
// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
|
||||
// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点
|
||||
// 如果index大于链表的长度,则返回空
|
||||
// 如果index小于0,则置为0,作为链表的新头节点。
|
||||
// 如果index小于0,则在头部插入节点
|
||||
void addAtIndex(int index, int val) {
|
||||
if (index > _size) {
|
||||
return;
|
||||
}
|
||||
if (index < 0) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
if(index > _size) return;
|
||||
if(index < 0) index = 0;
|
||||
LinkedNode* newNode = new LinkedNode(val);
|
||||
LinkedNode* cur = _dummyHead;
|
||||
while(index--) {
|
||||
|
@ -617,6 +617,75 @@ func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int]
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
> 100.相同的树
|
||||
|
||||
```rsut
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn is_same_tree(
|
||||
p: Option<Rc<RefCell<TreeNode>>>,
|
||||
q: Option<Rc<RefCell<TreeNode>>>,
|
||||
) -> bool {
|
||||
match (p, q) {
|
||||
(None, None) => true,
|
||||
(None, Some(_)) => false,
|
||||
(Some(_), None) => false,
|
||||
(Some(n1), Some(n2)) => {
|
||||
if n1.borrow().val == n2.borrow().val {
|
||||
let right =
|
||||
Self::is_same_tree(n1.borrow().left.clone(), n2.borrow().left.clone());
|
||||
let left =
|
||||
Self::is_same_tree(n1.borrow().right.clone(), n2.borrow().right.clone());
|
||||
right && left
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 257.二叉树的不同路径
|
||||
|
||||
```rust
|
||||
// 递归
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn binary_tree_paths(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
|
||||
let mut res = vec![];
|
||||
let mut path = vec![];
|
||||
Self::recur(&root, &mut path, &mut res);
|
||||
res
|
||||
}
|
||||
|
||||
pub fn recur(
|
||||
root: &Option<Rc<RefCell<TreeNode>>>,
|
||||
path: &mut Vec<String>,
|
||||
res: &mut Vec<String>,
|
||||
) {
|
||||
let node = root.as_ref().unwrap().borrow();
|
||||
path.push(node.val.to_string());
|
||||
if node.left.is_none() && node.right.is_none() {
|
||||
res.push(path.join("->"));
|
||||
return;
|
||||
}
|
||||
if node.left.is_some() {
|
||||
Self::recur(&node.left, path, res);
|
||||
path.pop(); //回溯
|
||||
}
|
||||
if node.right.is_some() {
|
||||
Self::recur(&node.right, path, res);
|
||||
path.pop(); //回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user