Skip to content

Commit 9f203fb

Browse files
Merge pull request youngyangyang04#2860 from kingemma/master
添加 0102.二叉树的层序遍历- 199.二叉树的右视图 和 637.二叉树的层平均值 C# 版本
2 parents 6731702 + aeb7834 commit 9f203fb

File tree

2 files changed

+127
-18
lines changed

2 files changed

+127
-18
lines changed

problems/0059.螺旋矩阵II.md

+57-18
Original file line numberDiff line numberDiff line change
@@ -715,26 +715,65 @@ object Solution {
715715
### C#:
716716

717717
```csharp
718-
public class Solution {
719-
public int[][] GenerateMatrix(int n) {
720-
int[][] answer = new int[n][];
721-
for(int i = 0; i < n; i++)
722-
answer[i] = new int[n];
723-
int start = 0;
724-
int end = n - 1;
725-
int tmp = 1;
726-
while(tmp < n * n)
718+
public int[][] GenerateMatrix(int n)
719+
{
720+
// 参考Carl的代码随想录里面C++的思路
721+
// https://www.programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#%E6%80%9D%E8%B7%AF
722+
int startX = 0, startY = 0; // 定义每循环一个圈的起始位置
723+
int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
724+
int count = 1; // 用来给矩阵每个空格赋值
725+
int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
726+
int offset = 1;// 需要控制每一条边遍历的长度,每次循环右边界收缩一位
727+
728+
// 构建result二维数组
729+
int[][] result = new int[n][];
730+
for (int k = 0; k < n; k++)
731+
{
732+
result[k] = new int[n];
733+
}
734+
735+
int i = 0, j = 0; // [i,j]
736+
while (loop > 0)
737+
{
738+
i = startX;
739+
j = startY;
740+
// 四个For循环模拟转一圈
741+
// 第一排,从左往右遍历,不取最右侧的值(左闭右开)
742+
for (; j < n - offset; j++)
743+
{
744+
result[i][j] = count++;
745+
}
746+
// 右侧的第一列,从上往下遍历,不取最下面的值(左闭右开)
747+
for (; i < n - offset; i++)
748+
{
749+
result[i][j] = count++;
750+
}
751+
752+
// 最下面的第一行,从右往左遍历,不取最左侧的值(左闭右开)
753+
for (; j > startY; j--)
754+
{
755+
result[i][j] = count++;
756+
}
757+
758+
// 左侧第一列,从下往上遍历,不取最左侧的值(左闭右开)
759+
for (; i > startX; i--)
727760
{
728-
for(int i = start; i < end; i++) answer[start][i] = tmp++;
729-
for(int i = start; i < end; i++) answer[i][end] = tmp++;
730-
for(int i = end; i > start; i--) answer[end][i] = tmp++;
731-
for(int i = end; i > start; i--) answer[i][start] = tmp++;
732-
start++;
733-
end--;
734-
}
735-
if(n % 2 == 1) answer[n / 2][n / 2] = tmp;
736-
return answer;
761+
result[i][j] = count++;
762+
}
763+
// 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
764+
startX++;
765+
startY++;
766+
767+
// offset 控制每一圈里每一条边遍历的长度
768+
offset++;
769+
loop--;
770+
}
771+
if (n % 2 == 1)
772+
{
773+
// n 为奇数
774+
result[mid][mid] = count;
737775
}
776+
return result;
738777
}
739778
```
740779

problems/0102.二叉树的层序遍历.md

+70
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,47 @@ impl Solution {
12311231
}
12321232
```
12331233

1234+
#### C#:
1235+
1236+
```C# 199.二叉树的右视图
1237+
public class Solution
1238+
{
1239+
public IList<int> RightSideView(TreeNode root)
1240+
{
1241+
var result = new List<int>();
1242+
Queue<TreeNode> queue = new();
1243+
1244+
if (root != null)
1245+
{
1246+
queue.Enqueue(root);
1247+
}
1248+
while (queue.Count > 0)
1249+
{
1250+
int count = queue.Count;
1251+
int lastValue = count - 1;
1252+
for (int i = 0; i < count; i++)
1253+
{
1254+
var currentNode = queue.Dequeue();
1255+
if (i == lastValue)
1256+
{
1257+
result.Add(currentNode.val);
1258+
}
1259+
1260+
// lastValue == i == count -1 : left 先于 right 进入Queue
1261+
if (currentNode.left != null) queue.Enqueue(currentNode.left);
1262+
if (currentNode.right != null) queue.Enqueue(currentNode.right);
1263+
1264+
//// lastValue == i == 0: right 先于 left 进入Queue
1265+
// if(currentNode.right !=null ) queue.Enqueue(currentNode.right);
1266+
// if(currentNode.left !=null ) queue.Enqueue(currentNode.left);
1267+
}
1268+
}
1269+
1270+
return result;
1271+
}
1272+
}
1273+
```
1274+
12341275
## 637.二叉树的层平均值
12351276

12361277
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
@@ -1558,6 +1599,35 @@ impl Solution {
15581599
}
15591600
```
15601601

1602+
#### C#:
1603+
1604+
```C# 二叉树的层平均值
1605+
public class Solution {
1606+
public IList<double> AverageOfLevels(TreeNode root) {
1607+
var result= new List<double>();
1608+
Queue<TreeNode> queue = new();
1609+
if(root !=null) queue.Enqueue(root);
1610+
1611+
while (queue.Count > 0)
1612+
{
1613+
int count = queue.Count;
1614+
double value=0;
1615+
for (int i = 0; i < count; i++)
1616+
{
1617+
var curentNode=queue.Dequeue();
1618+
value += curentNode.val;
1619+
if (curentNode.left!=null) queue.Enqueue(curentNode.left);
1620+
if (curentNode.right!=null) queue.Enqueue(curentNode.right);
1621+
}
1622+
result.Add(value/count);
1623+
}
1624+
1625+
return result;
1626+
}
1627+
}
1628+
1629+
```
1630+
15611631
## 429.N叉树的层序遍历
15621632

15631633
[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)

0 commit comments

Comments
 (0)