-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0199-BinaryTreeRightSideView.cs
39 lines (34 loc) · 1.13 KB
/
0199-BinaryTreeRightSideView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 30.2 MB
// Link: https://leetcode.com/submissions/detail/378614277/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0199_BinaryTreeRightSideView
{
public IList<int> RightSideView(TreeNode root)
{
var results = new List<int>();
if (root == null) return results;
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0)
{
var length = queue.Count;
TreeNode node = null;
for (int i = length; i > 0; i--)
{
node = queue.Dequeue();
if (node.left != null)
queue.Enqueue(node.left);
if (node.right != null)
queue.Enqueue(node.right);
}
results.Add(node.val);
}
return results;
}
}
}