-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_102.cs
34 lines (30 loc) · 948 Bytes
/
No_102.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
namespace LeetCode
{
public class No_102
{
public IList<IList<int>> LevelOrder(TreeNode root)
{
List<List<int>> ans = new List<List<int>>();
if (root == null)
return ans.ToArray();
Queue<TreeNode> next = new Queue<TreeNode>();
next.Enqueue(root);
while (next.Count != 0)
{
TreeNode[] n = new TreeNode[next.Count];
next.CopyTo(n, 0);
next.Clear();
ans.Add(new List<int>());
for (int i = 0; i < n.Length; i++)
{
ans.Last().Add(n[i].val);
if (n[i].left != null)
next.Enqueue(n[i].left);
if (n[i].right != null)
next.Enqueue(n[i].right);
}
}
return ans.ToArray();
}
}
}