Skip to content

Commit 3d50067

Browse files
committed
https://leetcode.com/problems/binary-tree-level-order-traversal/
1 parent a41d1ff commit 3d50067

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace LeetCodeSolutions.BinaryTreeBfs
2+
{
3+
class BinaryTreeLevelOrderTraversal
4+
{
5+
public IList<IList<int>> LevelOrder(TreeNode root)
6+
{
7+
Dictionary<int, IList<int>> dict = new Dictionary<int, IList<int>>();
8+
Process(root, 0, dict);
9+
return dict.Values.ToList();
10+
}
11+
12+
private void Process(TreeNode node, int level, Dictionary<int, IList<int>> dict)
13+
{
14+
if (node != null)
15+
{
16+
if (dict.ContainsKey(level))
17+
{
18+
dict[level].Add(node.val);
19+
}
20+
else
21+
{
22+
dict.Add(level, new List<int>() { node.val });
23+
}
24+
25+
Process(node.left, level + 1, dict);
26+
Process(node.right, level + 1, dict);
27+
}
28+
}
29+
30+
[Test(Description = "https://leetcode.com/problems/binary-tree-level-order-traversal/")]
31+
[Category("Medium")]
32+
[Category("LeetCode")]
33+
[Category("Binary Tree Level Order Traversal")]
34+
[TestCaseSource(nameof(Input))]
35+
[Category("Binary Tree BFS")]
36+
[Category("TopInterview")]
37+
public void Test1((IList<IList<int>> Output, TreeNode Input) item)
38+
{
39+
var response = this.LevelOrder(item.Input);
40+
Assert.That(response, Is.EqualTo(item.Output));
41+
}
42+
43+
public static IEnumerable<(IList<IList<int>> Output, TreeNode Input)> Input =>
44+
new List<(IList<IList<int>> Output, TreeNode Input)>()
45+
{
46+
(new List<IList<int>>()
47+
{
48+
new List<int>() { 3 },
49+
new List<int>() { 9, 20 },
50+
new List<int>() { 15, 7 },
51+
}, new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7))))
52+
};
53+
}
54+
}

LeetCodeSolutions/BinaryTreeBfs/BinaryTreeRightSideView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace LeetCode.MediumProblems;
1+
namespace LeetCodeSolutions.BinaryTreeBfs;
22

33
public class BinaryTreeRightSideView
44
{

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
136136
| <br> Binary Tree BFS<br> | | | |
137137
| 199 | Binary Tree Right Side View | Medium ||
138138
| 83 | Average of Levels in Binary Tree | Easy ||
139-
| 84 | Binary Tree Level Order Traversal | Medium | |
139+
| 84 | Binary Tree Level Order Traversal | Medium | |
140140
| 85 | Binary Tree Zigzag Level Order Traversal | Medium | |
141141
| <br> Binary Search Tree<br> | | | |
142142
| 86 | Minimum Absolute Difference in BST | Easy ||

0 commit comments

Comments
 (0)