Skip to content

Commit 47f9bdb

Browse files
committed
https://leetcode.com/problems/average-of-levels-in-binary-tree/
1 parent dc2ec6e commit 47f9bdb

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
namespace LeetCodeSolutions.BinaryTreeBfs;
3+
4+
public class AverageLevelsOfBinaryTree
5+
{
6+
public IList<double> AverageOfLevels(TreeNode root) {
7+
8+
Dictionary<int, Tuple<int, long>> map = new Dictionary<int, Tuple<int, long>>();
9+
_dfs(root, 0, map);
10+
11+
IList<double> result = new List<double>();
12+
foreach (var kvp in map)
13+
{
14+
result.Add((double)kvp.Value.Item2 / kvp.Value.Item1);
15+
}
16+
17+
return result;
18+
}
19+
20+
private void _dfs(TreeNode node, int depth, Dictionary<int, Tuple<int, long>> map)
21+
{
22+
if (node != null)
23+
{
24+
if (map.ContainsKey(depth))
25+
{
26+
var currentVal = map[depth];
27+
map[depth] = Tuple.Create(currentVal.Item1 + 1, currentVal.Item2 + node.val);
28+
}
29+
else
30+
{
31+
map[depth] = Tuple.Create(1, (long)node.val);
32+
}
33+
_dfs(node.left, depth + 1, map);
34+
_dfs(node.right, depth + 1, map);
35+
}
36+
}
37+
38+
[Test(Description = "https://leetcode.com/problems/average-of-levels-in-binary-tree")]
39+
[Category("Easy")]
40+
[Category("LeetCode")]
41+
[Category("Average Of Levels in Binary Tree")]
42+
[TestCaseSource(nameof(Input))]
43+
[Category("Binary Tree BFS")]
44+
[Category("TopInterview")]
45+
public void Test1((IList<double> Output, int?[] Input) item)
46+
{
47+
var response = AverageOfLevels(item.Input.ToTreeNode());
48+
Assert.That(response, Is.EqualTo(item.Output));
49+
}
50+
51+
public static IEnumerable<(IList<double> Output, int?[] Input)> Input =>
52+
new List<(IList<double> Output, int?[] Input)>()
53+
{
54+
([3.00000,14.50000,11.00000], [3,9,20,null,null,15,7]),
55+
([2147483647.00000,2147483647.00000], [2147483647,2147483647,2147483647]),
56+
};
57+
}

LeetCodeSolutions/Usings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
global using NUnit.Framework;
1+
global using NUnit.Framework;
2+
global using LeetCodeSolutions.Utils;

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![](https://img.shields.io/badge/Neovim-57A143.svg?logo=Neovim&amp;logoColor=white)
44
![](https://img.shields.io/badge/Visual_Studio_Code-0078D4?logo=visual%20studio%20code&amp;logoColor=white)
5-
![](https://img.shields.io/badge/Progress-28%2F150-0078D4)
5+
![](https://img.shields.io/badge/Progress-29%2F150-0078D4)
66

77
This repository contains solutions to the Leetcode Top Interview 150 problems.
88

@@ -135,7 +135,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
135135
| 81 | Lowest Common Ancestor of a Binary Tree | Medium | |
136136
| <br> Binary Tree BFS<br> | | | |
137137
| 8282 | Binary Tree Right Side View | Medium | |
138-
| 83 | Average of Levels in Binary Tree | Easy | |
138+
| 83 | Average of Levels in Binary Tree | Easy | |
139139
| 84 | Binary Tree Level Order Traversal | Medium | |
140140
| 85 | Binary Tree Zigzag Level Order Traversal | Medium | |
141141
| <br> Binary Search Tree<br> | | | |

0 commit comments

Comments
 (0)