|
d8009b9a4a
|
problems(rs): add “1431. Kids With the Greatest Number of Candies”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-17 18:51:10 +02:00 |
|
|
43d043b5e1
|
problems(rs): add “303. Range Sum Query - Immutable”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-16 00:05:53 +02:00 |
|
|
09a14a0e3d
|
problems(rs): add “2218. Maximum Value of K Coins From Piles”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-15 19:29:45 +02:00 |
|
|
01b25b0ffe
|
problems(rs): add “516. Longest Palindromic Subsequence”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-14 22:40:09 +02:00 |
|
|
2c3aa74565
|
problems(rs): add “946. Validate Stack Sequences”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-13 19:24:05 +02:00 |
|
|
551b7819cc
|
problems(rs): add “71. Simplify Path”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-13 08:13:07 +02:00 |
|
|
5a62aba9c1
|
problems(cpp): add “2390. Removing Stars From a String”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-11 18:47:54 +02:00 |
|
|
10b6b3dd69
|
problems(cpp): add “20. Valid Parentheses”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-10 20:03:58 +02:00 |
|
|
1388a820e7
|
problems(cpp): add “881. Boats to Save People”
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2023-04-10 12:21:07 +02:00 |
|
|
ec2b3df836
|
problems(cpp): add „100. Same Tree“
Signed-off-by: Matej Focko <me@mfocko.xyz>
|
2023-01-10 23:16:12 +01:00 |
|
|
63dad4c93c
|
problems(rs): add „451. Sort Characters By Frequency“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-12-03 22:41:54 +01:00 |
|
|
5d51c78a08
|
problems(cpp): add „1657. Determine if Two Strings Are Close“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-12-02 20:13:07 +01:00 |
|
|
fb8f4944c8
|
problems(cpp): add „393. UTF-8 Validation“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-14 10:31:37 +02:00 |
|
|
1700370e2f
|
problems(rs): add „188. Best Time to Buy and Sell Stock IV“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-10 22:33:42 +02:00 |
|
|
dd94963a2f
|
problems(cpp): add „1448. Count Good Nodes in Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-09 18:59:12 +02:00 |
|
|
f7ebb9037d
|
problems(kt): add „2133. Check if Every Row and Column Contains All Numbers“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-09 18:58:35 +02:00 |
|
|
e3888415f7
|
chore: fix formatting
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-09 18:56:13 +02:00 |
|
|
d7f8f4fd7b
|
problems(rs): add „1996. The Number of Weak Characters in the Game“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-09 14:02:07 +02:00 |
|
|
9f4f02ac24
|
problems(rs): add „94. Binary Tree Inorder Traversal“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-08 13:59:46 +02:00 |
|
|
4838f221f3
|
problems(kt): add „606. Construct String from Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-07 13:24:41 +02:00 |
|
|
20e3625bd7
|
problems(cs): add „814. Binary Tree Pruning“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-06 18:35:48 +02:00 |
|
|
23a0bb299a
|
problems(java): add „429. N-ary Tree Level Order Traversal“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-05 11:31:36 +02:00 |
|
|
0bb6581d69
|
problems(cpp): add „987. Vertical Order Traversal of a Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-04 23:02:44 +02:00 |
|
|
b9d382731b
|
problems(cpp): add „967. Numbers With Same Consecutive Differences“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-03 23:52:07 +02:00 |
|
|
227124b327
|
problems(cs): add „637. Average of Levels in Binary Tree“
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();
+ }
+}
|
2022-09-02 21:11:03 +02:00 |
|
|
280a674871
|
problems(rs): add „417. Pacific Atlantic Water Flow“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-01 01:01:53 +02:00 |
|
|
039c8b1a0b
|
problems(cpp): add „48. Rotate Image“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-09-01 01:01:22 +02:00 |
|
|
71b16b3ab8
|
problems(rs): add „200. Number of Islands“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-29 19:22:05 +02:00 |
|
|
5daf90ab2a
|
problems(cpp): add „1329. Sort the Matrix Diagonally“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-28 23:32:10 +02:00 |
|
|
72367000cd
|
problems(cpp): add „363. Max Sum of Rectangle No Larger Than K“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-28 23:31:42 +02:00 |
|
|
84eba00e33
|
problems(cpp): add „869. Reordered Power of 2“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-26 23:52:22 +02:00 |
|
|
4aea0bb137
|
problems(cpp): add „383. Ransom Note“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-25 14:06:33 +02:00 |
|
|
3bcb7469d9
|
problems(kt): add „326. Power of Three“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-24 12:35:09 +02:00 |
|
|
13c5b4b30a
|
problems(cpp): add „234. Palindrome Linked List“
Signed-off-by: Matej Focko <me@mfocko.xyz>
|
2022-08-24 00:04:31 +02:00 |
|
|
504fc1ba67
|
problems(rs): add „342. Power of Four“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-22 11:33:45 +02:00 |
|
|
10007a1821
|
problems(rs): add „936. Stamping The Sequence“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-21 20:19:10 +02:00 |
|
|
fef6742a2f
|
problems: add „871. Minimum Number of Refueling Stops“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-20 18:30:43 +02:00 |
|
|
6eb2f09892
|
problems: add „659. Split Array into Consecutive Subsequences“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-19 22:33:11 +02:00 |
|
|
1ccb92b8a7
|
problems: add „1338. Reduce Array Size to The Half“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-18 09:35:18 +02:00 |
|
|
3b7f75f4e8
|
problems: add „804. Unique Morse Code Words“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-17 11:48:21 +02:00 |
|
|
fa54cd1335
|
problems: add „387. First Unique Character in a String“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-16 11:10:52 +02:00 |
|
|
dbb22b755d
|
problems: add „13. Roman to Integer“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-15 09:45:52 +02:00 |
|
|
d533a1774f
|
problems: add „126. Word Ladder II“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-14 21:19:49 +02:00 |
|
|
66888a4109
|
problems: add „30. Substring with Concatenation of All Words“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-13 22:50:35 +02:00 |
|
|
9fa6578ce7
|
problems: add „235. Lowest Common Ancestor of a Binary Search Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-12 22:10:33 +02:00 |
|
|
15e6d58b12
|
problems: add „98. Validate Binary Search Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-11 10:02:47 +02:00 |
|
|
e11a347989
|
problems: add „108. Convert Sorted Array to Binary Search Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-10 20:58:57 +02:00 |
|
|
dd3b626157
|
problems: add „1286. Iterator for Combination“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-09 15:14:12 +02:00 |
|
|
234ae9fcf2
|
problems: add „823. Binary Trees With Factors“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-09 12:14:37 +02:00 |
|
|
33dbe9bc56
|
problems: add „Longest Increasing Subsequence“
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-08 21:58:30 +02:00 |
|
|
aba5f5f595
|
problems: add count vowels permutation
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-07 12:39:11 +02:00 |
|
|
aac030d0e2
|
problems: add poor pigs
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-06 20:59:39 +02:00 |
|
|
55ffc32fed
|
problems: add an RLE Iterator
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-06 17:34:50 +02:00 |
|
|
1f5f1bee8c
|
fix: correct the comments and interface
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-05 16:52:51 +02:00 |
|
|
5dd35539a5
|
problems: add flatten nested list iterator
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-05 16:51:33 +02:00 |
|
|
a71684d500
|
problems: add peeking iterator
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-05 12:11:01 +02:00 |
|
|
9dc591147a
|
problems: add combination sum IV
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-05 11:06:37 +02:00 |
|
|
aad7d85fd2
|
problems: add BST iterator
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-04 23:11:25 +02:00 |
|
|
826b402dd1
|
problems: add mirror reflection
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-04 21:35:42 +02:00 |
|
|
08a623bb50
|
problems: add my calendar I
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-03 19:30:14 +02:00 |
|
|
4845dae4b3
|
problems: add kth smallest element in a sorted matrix
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-02 22:46:54 +02:00 |
|
|
05c1800c4d
|
problems: add unique paths
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-08-02 11:40:31 +02:00 |
|
|
917aecb33f
|
chore: format sources
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-23 13:12:05 +02:00 |
|
|
b59f949d8f
|
problems: add n-ary tree preorder traversal
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-23 13:11:02 +02:00 |
|
|
7c3f9ba87e
|
problems: add n-ary tree level-order traversal
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-23 13:10:25 +02:00 |
|
|
6c3cfcd876
|
chore: reorganize files
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-23 12:53:31 +02:00 |
|
|
229e74a381
|
problems: add partition list
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-22 23:45:16 +02:00 |
|
|
d83859da38
|
problems: add number of matching subsequences
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-20 17:09:12 +02:00 |
|
|
891c50440e
|
problems: add pascals triangle ii
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-19 16:48:05 +02:00 |
|
|
123c4ef5d7
|
problems: add pascals triangle
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-07-19 16:26:56 +02:00 |
|
|
d1e2d4a9cf
|
problems: add out of boundary paths
|
2022-07-18 17:40:53 +00:00 |
|
|
a7f5724d71
|
problems: add max area of island
|
2022-07-16 14:31:55 +00:00 |
|
|
852221b295
|
problems: add maximum units on a truck
|
2022-07-14 13:28:35 +00:00 |
|
|
7929858e64
|
problems: add binary tree level-order traversal
|
2022-07-13 22:13:48 +00:00 |
|
|
671fe591f6
|
problems: add middle of the linked list
|
2022-07-12 19:09:21 +00:00 |
|
|
b600d8cc67
|
problems: add maximum numbers of coins you can get
|
2022-07-12 15:35:23 +00:00 |
|
|
af0dd6af82
|
problems: add find pivot index
|
2022-07-12 12:36:28 +00:00 |
|
|
b100c7588b
|
problems: add running sum of 1D array
|
2022-07-12 11:32:59 +00:00 |
|
|
9fa39e8873
|
data-structure(i): add day 1
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-19 18:25:31 +01:00 |
|
|
afd4b06f50
|
problems: add daily problems
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-19 12:19:22 +01:00 |
|
|
96c6e8d66d
|
problems: add previously solved problems
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-18 21:43:49 +01:00 |
|
|
3460ef6dbd
|
problems: add inorder bst traversal
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-18 16:17:04 +01:00 |
|
|
5a9d67f37d
|
problems: add can place flowers
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-18 13:38:21 +01:00 |
|
|
2c664a305d
|
problems: add maximize distance to closest
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-16 12:42:13 +01:00 |
|
|
16763cdd2f
|
problems: add insert-into-a-bst
Signed-off-by: Matej Focko <mfocko@redhat.com>
|
2022-01-12 20:10:00 +01:00 |
|