Matej Focko
227124b327
Signed-off-by: Matej Focko <mfocko@redhat.com> diff --git a/problems/average-of-levels-in-binary-tree.cs b/problems/average-of-levels-in-binary-tree.cs new file mode 100644 index 0000000..f2ba5aa --- /dev/null +++ b/problems/average-of-levels-in-binary-tree.cs @@ -0,0 +1,72 @@ +public class TreeNode +{ + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null) + { + this.val = val; + this.left = left; + this.right = right; + } +} + + +public class Solution +{ + private class Level + { + long sum; + int counter; + + public Level() + { + sum = 0; + counter = 0; + } + + public void Add(int x) + { + sum += x; + counter++; + } + + public double Average + { + get => sum / (double)counter; + } + } + + private List<Level> AverageOfLevels(List<Level> averages, TreeNode? node, int level) + { + if (node == null) + { + return averages; + } + + if (level == averages.Count) + { + averages.Add(new Level()); + } + averages[level].Add(node.val); + + AverageOfLevels(averages, node.left, level + 1); + AverageOfLevels(averages, node.right, level + 1); + + return averages; + } + + public IList<double> AverageOfLevels(TreeNode? root) + => AverageOfLevels(new List<Level>(), root, 0).Select(level => level.Average).ToList(); + + public static void Main() + { + var s = new Solution(); + + foreach (var a in s.AverageOfLevels(new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7))))) + { + Console.Write($"{a} "); + } + Console.WriteLine(); + } +} |
||
---|---|---|
.. | ||
average-of-levels-in-binary-tree.cs | ||
binary-search-tree-iterator.java | ||
binary-search.rb | ||
binary-tree-inorder-traversal.kt | ||
binary-tree-level-order-traversal.kt | ||
binary-trees-with-factors.rs | ||
can-place-flowers.cpp | ||
climbing-stairs.rb | ||
combination-sum-iv.java | ||
complex-number-multiplication.rs | ||
contains-duplicate.rb | ||
contains-duplicate.swift | ||
convert-sorted-array-to-binary-search-tree.java | ||
count-vowels-permutation.rs | ||
divide-a-string-into-groups-of-size-k.cpp | ||
fibonacci-number.rb | ||
find-pivot-index.rs | ||
first-bad-version.rb | ||
first-unique-character-in-a-string.kt | ||
flatten-nested-list-iterator.java | ||
insert-into-a-binary-search-tree.rb | ||
iterator-for-combination.kt | ||
kth-smallest-element-in-a-sorted-matrix.cpp | ||
linked-list-cycle-ii.rb | ||
longest-increasing-subsequence.rs | ||
lowest-common-ancestor-of-a-binary-search-tree.kt | ||
max-area-of-island.kt | ||
max-sum-of-rectangle-no-larger-than-k.cpp | ||
maximize-distance-to-closest-person.kt | ||
maximum-number-of-coins-you-can-get.rs | ||
maximum-running-time-of-n-computers.cpp | ||
maximum-subarray.rb | ||
maximum-units-on-a-truck.kt | ||
merge-sorted-array.rb | ||
middle-of-the-linked-list.cpp | ||
min-cost-climbing-stairs.rb | ||
minimum-moves-to-reach-target-score.cpp | ||
minimum-number-of-refueling-stops.kt | ||
mirror-reflection.cpp | ||
my-calendar-i.cpp | ||
n-ary-tree-level-order-traversal.cpp | ||
n-ary-tree-preorder-traversal.cpp | ||
number-complement.swift | ||
number-of-islands.rs | ||
number-of-matching-subsequences.cpp | ||
out-of-boundary-paths.cpp | ||
pacific-atlantic-water-flow.rs | ||
palindrome-linked-list.cpp | ||
partition-list.cpp | ||
pascals-triangle-ii.cpp | ||
pascals-triangle.cpp | ||
peeking-iterator.java | ||
poor-pigs.cs | ||
power-of-four.rs | ||
power-of-three.kt | ||
ransom-note.cpp | ||
reduce-array-size-to-the-half.rs | ||
reordered-power-of-2.cpp | ||
rle-iterator.java | ||
roman-to-integer.kt | ||
rotate-array.rb | ||
rotate-image.cpp | ||
running-sum-of-1d-array.rs | ||
search-insert-position.rb | ||
solving-questions-with-brainpower.cpp | ||
sort-the-matrix-diagonally.cpp | ||
split-array-into-consecutive-subsequences.kt | ||
squares-of-a-sorted-array.rb | ||
stamping-the-sequence.rs | ||
substring-with-concatenation-of-all-words.kt | ||
tribonacci.rb | ||
two-sum.rb | ||
unique-morse-code-words.kt | ||
unique-paths.cpp | ||
validate-binary-search-tree.kt | ||
word-ladder-ii.kt | ||
word-pattern.rb |