mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
添加 0102.二叉树的层序遍历--199.二叉树的右视图 C# 代码
0102.二叉树的层序遍历中的199.二叉树的右视图 C# 代码
This commit is contained in:
@ -1231,6 +1231,47 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
#### C#:
|
||||
|
||||
```C# 199.二叉树的右视图
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> RightSideView(TreeNode root)
|
||||
{
|
||||
var result = new List<int>();
|
||||
Queue<TreeNode> queue = new();
|
||||
|
||||
if (root != null)
|
||||
{
|
||||
queue.Enqueue(root);
|
||||
}
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
int count = queue.Count;
|
||||
int lastValue = count - 1;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var currentNode = queue.Dequeue();
|
||||
if (i == lastValue)
|
||||
{
|
||||
result.Add(currentNode.val);
|
||||
}
|
||||
|
||||
// lastValue == i == count -1 : left 先于 right 进入Queue
|
||||
if (currentNode.left != null) queue.Enqueue(currentNode.left);
|
||||
if (currentNode.right != null) queue.Enqueue(currentNode.right);
|
||||
|
||||
//// lastValue == i == 0: right 先于 left 进入Queue
|
||||
// if(currentNode.right !=null ) queue.Enqueue(currentNode.right);
|
||||
// if(currentNode.left !=null ) queue.Enqueue(currentNode.left);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 637.二叉树的层平均值
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
|
||||
|
Reference in New Issue
Block a user