generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem20.cs
41 lines (34 loc) · 1.1 KB
/
Problem20.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
40
41
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/binary-tree-level-order-traversal/">Binary Tree Level Order Traversal</see>.
/// </summary>
public static class Problem20
{
/// <summary>
/// Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="root">Binary tree root.</param>
/// <returns>Traversed values.</returns>
public static IList<IList<int>> LevelOrder(TreeNode? root)
{
var result = new List<IList<int>>();
Traverse(root, 0, result);
return result;
}
private static void Traverse(TreeNode? node, int level, IList<IList<int>> result)
{
if (node == null)
{
return;
}
if (level == result.Count)
{
result.Add(new List<int>());
}
result[level].Add(node.Val);
Traverse(node.Left, level + 1, result);
Traverse(node.Right, level + 1, result);
}
}