mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -1033,6 +1033,9 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
### C#
|
### C#
|
||||||
|
|
||||||
|
0104.二叉树的最大深度
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// 递归法
|
// 递归法
|
||||||
public int MaxDepth(TreeNode root) {
|
public int MaxDepth(TreeNode root) {
|
||||||
@ -1088,7 +1091,76 @@ public int MaxDepth(TreeNode root)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
559.n叉树的最大深度
|
||||||
|
递归法
|
||||||
|
```csharp
|
||||||
|
/*
|
||||||
|
递归法
|
||||||
|
*/
|
||||||
|
public class Solution {
|
||||||
|
public int MaxDepth(Node root) {
|
||||||
|
int res = 0;
|
||||||
|
/* 终止条件 */
|
||||||
|
if(root == null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* logic */
|
||||||
|
// 遍历当前节点的子节点
|
||||||
|
for (int i = 0; i < root.children.Count; i++)
|
||||||
|
{
|
||||||
|
res = Math.Max(res, MaxDepth(root.children[i]));
|
||||||
|
}
|
||||||
|
return res + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
```
|
||||||
|
迭代法(层序遍历)
|
||||||
|
```csharp
|
||||||
|
/*
|
||||||
|
迭代法
|
||||||
|
*/
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxDepth(Node root)
|
||||||
|
{
|
||||||
|
Queue<Node> que = new Queue<Node>(); // 使用泛型队列存储节点
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
if(root != null){
|
||||||
|
que.Enqueue(root); // 将根节点加入队列
|
||||||
|
}
|
||||||
|
while (que.Count > 0)
|
||||||
|
{
|
||||||
|
int size = que.Count; // 获取当前层的节点数
|
||||||
|
res++; // 深度加一
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
// 每一层的遍历
|
||||||
|
|
||||||
|
var curNode = que.Dequeue(); // 取出队列中的节点
|
||||||
|
for (int j = 0; j < curNode.children.Count; j++)
|
||||||
|
{
|
||||||
|
if (curNode.children[j] != null)
|
||||||
|
{
|
||||||
|
que.Enqueue(curNode.children[j]); // 将子节点加入队列
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 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"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -1523,8 +1523,64 @@ public bool HasPathSum(TreeNode root, int targetSum)
|
|||||||
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
|
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
0113.路径总和:
|
||||||
|
```csharp
|
||||||
|
/*
|
||||||
|
* @lc app=leetcode id=113 lang=csharp
|
||||||
|
* 0113.路径总和 II
|
||||||
|
* [113] Path Sum II
|
||||||
|
* 递归法
|
||||||
|
*/
|
||||||
|
public class Solution {
|
||||||
|
private List<List<int>> result = new List<List<int>>();
|
||||||
|
private List<int> path = new List<int>();
|
||||||
|
|
||||||
|
// Main function to find paths with the given sum
|
||||||
|
public IList<IList<int>> PathSum(TreeNode root, int targetSum) {
|
||||||
|
result.Clear();
|
||||||
|
path.Clear();
|
||||||
|
if (root == null) return result.Select(list => list as IList<int>).ToList();
|
||||||
|
path.Add(root.val); // Add the root node to the path
|
||||||
|
traversal(root, targetSum - root.val); // Start the traversal
|
||||||
|
return result.Select(list => list as IList<int>).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive function to traverse the tree and find paths
|
||||||
|
private void traversal(TreeNode node, int count) {
|
||||||
|
// If a leaf node is reached and the target sum is achieved
|
||||||
|
if (node.left == null && node.right == null && count == 0) {
|
||||||
|
result.Add(new List<int>(path)); // Add a copy of the path to the result
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a leaf node is reached and the target sum is not achieved, or if it's not a leaf node
|
||||||
|
if (node.left == null && node.right == null) return;
|
||||||
|
|
||||||
|
// Traverse the left subtree
|
||||||
|
if (node.left != null) {
|
||||||
|
path.Add(node.left.val);
|
||||||
|
count -= node.left.val;
|
||||||
|
traversal(node.left, count); // Recursive call
|
||||||
|
count += node.left.val; // Backtrack
|
||||||
|
path.RemoveAt(path.Count - 1); // Backtrack
|
||||||
|
}
|
||||||
|
|
||||||
|
// Traverse the right subtree
|
||||||
|
if (node.right != null) {
|
||||||
|
path.Add(node.right.val);
|
||||||
|
count -= node.right.val;
|
||||||
|
traversal(node.right, count); // Recursive call
|
||||||
|
count += node.right.val; // Backtrack
|
||||||
|
path.RemoveAt(path.Count - 1); // Backtrack
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
|
```
|
||||||
<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"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -562,7 +562,213 @@ function pacificAtlantic(heights) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### go
|
||||||
|
|
||||||
|
dfs:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
|
||||||
|
|
||||||
|
func pacificAtlantic(heights [][]int) [][]int {
|
||||||
|
res := make([][]int, 0)
|
||||||
|
pacific := make([][]bool, len(heights))
|
||||||
|
atlantic := make([][]bool, len(heights))
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
pacific[i] = make([]bool, len(heights[0]))
|
||||||
|
atlantic[i] = make([]bool, len(heights[0]))
|
||||||
|
}
|
||||||
|
// 列
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
dfs(heights, pacific, i, 0)
|
||||||
|
dfs(heights, atlantic, i, len(heights[0])-1)
|
||||||
|
}
|
||||||
|
// 行
|
||||||
|
for j := 0; j < len(heights[0]); j++ {
|
||||||
|
dfs(heights, pacific, 0, j)
|
||||||
|
dfs(heights, atlantic, len(heights)-1, j)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
for j := 0; j < len(heights[0]); j++ {
|
||||||
|
if pacific[i][j] && atlantic[i][j] {
|
||||||
|
res = append(res, []int{i, j})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func dfs(heights [][]int, visited [][]bool, i, j int) {
|
||||||
|
visited[i][j] = true
|
||||||
|
for _, d := range DIRECTIONS {
|
||||||
|
x, y := i+d[0], j+d[1]
|
||||||
|
if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[i][j] > heights[x][y] || visited[x][y] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs(heights, visited, x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
bfs:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
|
||||||
|
|
||||||
|
func pacificAtlantic(heights [][]int) [][]int {
|
||||||
|
res := make([][]int, 0)
|
||||||
|
pacific := make([][]bool, len(heights))
|
||||||
|
atlantic := make([][]bool, len(heights))
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
pacific[i] = make([]bool, len(heights[0]))
|
||||||
|
atlantic[i] = make([]bool, len(heights[0]))
|
||||||
|
}
|
||||||
|
// 列
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
bfs(heights, pacific, i, 0)
|
||||||
|
bfs(heights, atlantic, i, len(heights[0])-1)
|
||||||
|
}
|
||||||
|
// 行
|
||||||
|
for j := 0; j < len(heights[0]); j++ {
|
||||||
|
bfs(heights, pacific, 0, j)
|
||||||
|
bfs(heights, atlantic, len(heights)-1, j)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(heights); i++ {
|
||||||
|
for j := 0; j < len(heights[0]); j++ {
|
||||||
|
if pacific[i][j] && atlantic[i][j] {
|
||||||
|
res = append(res, []int{i, j})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func bfs(heights [][]int, visited [][]bool, i, j int) {
|
||||||
|
queue := make([][]int, 0)
|
||||||
|
queue = append(queue, []int{i, j})
|
||||||
|
visited[i][j] = true
|
||||||
|
for len(queue) > 0 {
|
||||||
|
cur := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
for _, d := range DIRECTIONS {
|
||||||
|
x, y := cur[0]+d[0], cur[1]+d[1]
|
||||||
|
if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[cur[0]][cur[1]] > heights[x][y] || visited[x][y] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
queue = append(queue, []int{x, y})
|
||||||
|
visited[x][y] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
dfs:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
|
||||||
|
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||||
|
let (m, n) = (heights.len(), heights[0].len());
|
||||||
|
let mut res = vec![];
|
||||||
|
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
|
||||||
|
|
||||||
|
// 列
|
||||||
|
for i in 0..m {
|
||||||
|
Self::dfs(&heights, &mut pacific, i, 0);
|
||||||
|
Self::dfs(&heights, &mut atlantic, i, n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for j in 0..n {
|
||||||
|
Self::dfs(&heights, &mut pacific, 0, j);
|
||||||
|
Self::dfs(&heights, &mut atlantic, m - 1, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..m {
|
||||||
|
for j in 0..n {
|
||||||
|
if pacific[i][j] && atlantic[i][j] {
|
||||||
|
res.push(vec![i as i32, j as i32]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
|
||||||
|
visited[i][j] = true;
|
||||||
|
for (dx, dy) in Self::DIRECTIONS {
|
||||||
|
let (x, y) = (i as isize + dx, j as isize + dy);
|
||||||
|
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (x, y) = (x as usize, y as usize);
|
||||||
|
if !visited[x][y] && heights[x][y] >= heights[i][j] {
|
||||||
|
Self::dfs(heights, visited, x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
bfs:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
|
||||||
|
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||||
|
let (m, n) = (heights.len(), heights[0].len());
|
||||||
|
let mut res = vec![];
|
||||||
|
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
|
||||||
|
|
||||||
|
// 列
|
||||||
|
for i in 0..m {
|
||||||
|
Self::bfs(&heights, &mut pacific, i, 0);
|
||||||
|
Self::bfs(&heights, &mut atlantic, i, n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for j in 0..n {
|
||||||
|
Self::bfs(&heights, &mut pacific, 0, j);
|
||||||
|
Self::bfs(&heights, &mut atlantic, m - 1, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..m {
|
||||||
|
for j in 0..n {
|
||||||
|
if pacific[i][j] && atlantic[i][j] {
|
||||||
|
res.push(vec![i as i32, j as i32]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
|
||||||
|
let mut queue = VecDeque::from([(i, j)]);
|
||||||
|
visited[i][j] = true;
|
||||||
|
while let Some((i, j)) = queue.pop_front() {
|
||||||
|
for (dx, dy) in Self::DIRECTIONS {
|
||||||
|
let (x, y) = (i as isize + dx, j as isize + dy);
|
||||||
|
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (x, y) = (x as usize, y as usize);
|
||||||
|
if !visited[x][y] && heights[x][y] >= heights[i][j] {
|
||||||
|
queue.push_back((x, y));
|
||||||
|
visited[x][y] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<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">
|
||||||
|
@ -716,9 +716,55 @@ public void Traversal(TreeNode root, int depth)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```csharp
|
||||||
|
/*
|
||||||
|
* @lc app=leetcode id=513 lang=csharp
|
||||||
|
* 迭代法
|
||||||
|
* [513] Find Bottom Left Tree Value
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int FindBottomLeftValue(TreeNode root)
|
||||||
|
{
|
||||||
|
Queue<TreeNode> que = new Queue<TreeNode>();
|
||||||
|
|
||||||
|
if (root != null)
|
||||||
|
{
|
||||||
|
que.Enqueue(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ans = 0;
|
||||||
|
while (que.Count != 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
int size = que.Count;
|
||||||
|
for (var i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var curNode = que.Peek();
|
||||||
|
que.Dequeue();
|
||||||
|
if(i == 0){
|
||||||
|
ans = curNode.val;
|
||||||
|
}
|
||||||
|
if (curNode.left != null)
|
||||||
|
{
|
||||||
|
que.Enqueue(curNode.left);
|
||||||
|
}
|
||||||
|
if (curNode.right != null)
|
||||||
|
{
|
||||||
|
que.Enqueue(curNode.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
```
|
||||||
|
|
||||||
<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"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user