Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57cf171

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 1bd8143 + 668aff7 commit 57cf171

File tree

4 files changed

+381
-1
lines changed

4 files changed

+381
-1
lines changed

‎problems/0104.二叉树的最大深度.md‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,9 @@ impl Solution {
10331033
}
10341034
```
10351035
### C#
1036+
1037+
0104.二叉树的最大深度
1038+
10361039
```csharp
10371040
// 递归法
10381041
public int MaxDepth(TreeNode root) {
@@ -1088,7 +1091,76 @@ public int MaxDepth(TreeNode root)
10881091
}
10891092
```
10901093

1094+
559.n叉树的最大深度
1095+
递归法
1096+
```csharp
1097+
/*
1098+
递归法
1099+
*/
1100+
public class Solution {
1101+
public int MaxDepth(Node root) {
1102+
int res = 0;
1103+
/* 终止条件 */
1104+
if(root == null){
1105+
return 0;
1106+
}
1107+
1108+
/* logic */
1109+
// 遍历当前节点的子节点
1110+
for (int i = 0; i < root.children.Count; i++)
1111+
{
1112+
res = Math.Max(res, MaxDepth(root.children[i]));
1113+
}
1114+
return res + 1;
1115+
}
1116+
}
1117+
// @lc code=end
1118+
```
1119+
迭代法(层序遍历)
1120+
```csharp
1121+
/*
1122+
迭代法
1123+
*/
1124+
public class Solution
1125+
{
1126+
public int MaxDepth(Node root)
1127+
{
1128+
Queue<Node> que = new Queue<Node>(); // 使用泛型队列存储节点
1129+
1130+
int res = 0;
1131+
1132+
if(root != null){
1133+
que.Enqueue(root); // 将根节点加入队列
1134+
}
1135+
while (que.Count > 0)
1136+
{
1137+
int size = que.Count; // 获取当前层的节点数
1138+
res++; // 深度加一
1139+
1140+
for (int i = 0; i < size; i++)
1141+
{
1142+
// 每一层的遍历
1143+
1144+
var curNode = que.Dequeue(); // 取出队列中的节点
1145+
for (int j = 0; j < curNode.children.Count; j++)
1146+
{
1147+
if (curNode.children[j] != null)
1148+
{
1149+
que.Enqueue(curNode.children[j]); // 将子节点加入队列
1150+
}
1151+
}
1152+
}
1153+
}
1154+
1155+
return res; // 返回树的最大深度
1156+
1157+
}
1158+
}
1159+
```
1160+
1161+
10911162
<p align="center">
10921163
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
10931164
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
10941165
</a>
1166+

‎problems/0112.路径总和.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,8 +1523,64 @@ public bool HasPathSum(TreeNode root, int targetSum)
15231523
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
15241524
}
15251525
```
1526+
0113.路径总和:
1527+
```csharp
1528+
/*
1529+
* @lc app=leetcode id=113 lang=csharp
1530+
* 0113.路径总和 II
1531+
* [113] Path Sum II
1532+
* 递归法
1533+
*/
1534+
public class Solution {
1535+
private List<List<int>> result = new List<List<int>>();
1536+
private List<int> path = new List<int>();
1537+
1538+
// Main function to find paths with the given sum
1539+
public IList<IList<int>> PathSum(TreeNode root, int targetSum) {
1540+
result.Clear();
1541+
path.Clear();
1542+
if (root == null) return result.Select(list => list as IList<int>).ToList();
1543+
path.Add(root.val); // Add the root node to the path
1544+
traversal(root, targetSum - root.val); // Start the traversal
1545+
return result.Select(list => list as IList<int>).ToList();
1546+
}
1547+
1548+
// Recursive function to traverse the tree and find paths
1549+
private void traversal(TreeNode node, int count) {
1550+
// If a leaf node is reached and the target sum is achieved
1551+
if (node.left == null && node.right == null && count == 0) {
1552+
result.Add(new List<int>(path)); // Add a copy of the path to the result
1553+
return;
1554+
}
1555+
1556+
// If a leaf node is reached and the target sum is not achieved, or if it's not a leaf node
1557+
if (node.left == null && node.right == null) return;
1558+
1559+
// Traverse the left subtree
1560+
if (node.left != null) {
1561+
path.Add(node.left.val);
1562+
count -= node.left.val;
1563+
traversal(node.left, count); // Recursive call
1564+
count += node.left.val; // Backtrack
1565+
path.RemoveAt(path.Count - 1); // Backtrack
1566+
}
1567+
1568+
// Traverse the right subtree
1569+
if (node.right != null) {
1570+
path.Add(node.right.val);
1571+
count -= node.right.val;
1572+
traversal(node.right, count); // Recursive call
1573+
count += node.right.val; // Backtrack
1574+
path.RemoveAt(path.Count - 1); // Backtrack
1575+
}
1576+
}
1577+
}
1578+
1579+
// @lc code=end
15261580
1581+
```
15271582
<p align="center">
15281583
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
15291584
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
15301585
</a>
1586+

‎problems/0417.太平洋大西洋水流问题.md‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,213 @@ function pacificAtlantic(heights) {
562562
}
563563
```
564564

565+
### go
566+
567+
dfs:
568+
569+
```go
570+
var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
571+
572+
func pacificAtlantic(heights [][]int) [][]int {
573+
res := make([][]int, 0)
574+
pacific := make([][]bool, len(heights))
575+
atlantic := make([][]bool, len(heights))
576+
for i := 0; i < len(heights); i++ {
577+
pacific[i] = make([]bool, len(heights[0]))
578+
atlantic[i] = make([]bool, len(heights[0]))
579+
}
580+
//
581+
for i := 0; i < len(heights); i++ {
582+
dfs(heights, pacific, i, 0)
583+
dfs(heights, atlantic, i, len(heights[0])-1)
584+
}
585+
//
586+
for j := 0; j < len(heights[0]); j++ {
587+
dfs(heights, pacific, 0, j)
588+
dfs(heights, atlantic, len(heights)-1, j)
589+
}
590+
591+
for i := 0; i < len(heights); i++ {
592+
for j := 0; j < len(heights[0]); j++ {
593+
if pacific[i][j] && atlantic[i][j] {
594+
res = append(res, []int{i, j})
595+
}
596+
}
597+
}
598+
599+
return res
600+
}
601+
602+
func dfs(heights [][]int, visited [][]bool, i, j int) {
603+
visited[i][j] = true
604+
for _, d := range DIRECTIONS {
605+
x, y := i+d[0], j+d[1]
606+
if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[i][j] > heights[x][y] || visited[x][y] {
607+
continue
608+
}
609+
610+
dfs(heights, visited, x, y)
611+
}
612+
}
613+
```
614+
615+
bfs:
616+
617+
```go
618+
var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
619+
620+
func pacificAtlantic(heights [][]int) [][]int {
621+
res := make([][]int, 0)
622+
pacific := make([][]bool, len(heights))
623+
atlantic := make([][]bool, len(heights))
624+
for i := 0; i < len(heights); i++ {
625+
pacific[i] = make([]bool, len(heights[0]))
626+
atlantic[i] = make([]bool, len(heights[0]))
627+
}
628+
//
629+
for i := 0; i < len(heights); i++ {
630+
bfs(heights, pacific, i, 0)
631+
bfs(heights, atlantic, i, len(heights[0])-1)
632+
}
633+
//
634+
for j := 0; j < len(heights[0]); j++ {
635+
bfs(heights, pacific, 0, j)
636+
bfs(heights, atlantic, len(heights)-1, j)
637+
}
638+
639+
for i := 0; i < len(heights); i++ {
640+
for j := 0; j < len(heights[0]); j++ {
641+
if pacific[i][j] && atlantic[i][j] {
642+
res = append(res, []int{i, j})
643+
}
644+
}
645+
}
646+
647+
return res
648+
}
649+
650+
func bfs(heights [][]int, visited [][]bool, i, j int) {
651+
queue := make([][]int, 0)
652+
queue = append(queue, []int{i, j})
653+
visited[i][j] = true
654+
for len(queue) > 0 {
655+
cur := queue[0]
656+
queue = queue[1:]
657+
for _, d := range DIRECTIONS {
658+
x, y := cur[0]+d[0], cur[1]+d[1]
659+
if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[cur[0]][cur[1]] > heights[x][y] || visited[x][y] {
660+
continue
661+
}
662+
queue = append(queue, []int{x, y})
663+
visited[x][y] = true
664+
}
665+
}
666+
}
667+
```
668+
669+
### Rust
670+
671+
dfs:
565672

673+
```rust
674+
impl Solution {
675+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
676+
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
677+
let (m, n) = (heights.len(), heights[0].len());
678+
let mut res = vec![];
679+
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
680+
681+
//
682+
for i in 0..m {
683+
Self::dfs(&heights, &mut pacific, i, 0);
684+
Self::dfs(&heights, &mut atlantic, i, n - 1);
685+
}
686+
687+
for j in 0..n {
688+
Self::dfs(&heights, &mut pacific, 0, j);
689+
Self::dfs(&heights, &mut atlantic, m - 1, j);
690+
}
691+
692+
for i in 0..m {
693+
for j in 0..n {
694+
if pacific[i][j] && atlantic[i][j] {
695+
res.push(vec![i as i32, j as i32]);
696+
}
697+
}
698+
}
699+
700+
res
701+
}
702+
703+
pub fn dfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
704+
visited[i][j] = true;
705+
for (dx, dy) in Self::DIRECTIONS {
706+
let (x, y) = (i as isize + dx, j as isize + dy);
707+
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
708+
continue;
709+
}
710+
let (x, y) = (x as usize, y as usize);
711+
if !visited[x][y] && heights[x][y] >= heights[i][j] {
712+
Self::dfs(heights, visited, x, y);
713+
}
714+
}
715+
}
716+
}
717+
```
718+
719+
bfs:
720+
721+
```rust
722+
use std::collections::VecDeque;
723+
724+
impl Solution {
725+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
726+
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
727+
let (m, n) = (heights.len(), heights[0].len());
728+
let mut res = vec![];
729+
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
730+
731+
//
732+
for i in 0..m {
733+
Self::bfs(&heights, &mut pacific, i, 0);
734+
Self::bfs(&heights, &mut atlantic, i, n - 1);
735+
}
736+
737+
for j in 0..n {
738+
Self::bfs(&heights, &mut pacific, 0, j);
739+
Self::bfs(&heights, &mut atlantic, m - 1, j);
740+
}
741+
742+
for i in 0..m {
743+
for j in 0..n {
744+
if pacific[i][j] && atlantic[i][j] {
745+
res.push(vec![i as i32, j as i32]);
746+
}
747+
}
748+
}
749+
750+
res
751+
}
752+
753+
pub fn bfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
754+
let mut queue = VecDeque::from([(i, j)]);
755+
visited[i][j] = true;
756+
while let Some((i, j)) = queue.pop_front() {
757+
for (dx, dy) in Self::DIRECTIONS {
758+
let (x, y) = (i as isize + dx, j as isize + dy);
759+
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
760+
continue;
761+
}
762+
let (x, y) = (x as usize, y as usize);
763+
if !visited[x][y] && heights[x][y] >= heights[i][j] {
764+
queue.push_back((x, y));
765+
visited[x][y] = true;
766+
}
767+
}
768+
}
769+
}
770+
}
771+
```
566772

567773
<p align="center">
568774
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /