File tree Expand file tree Collapse file tree 1 file changed +30
-1
lines changed
src/test/kotlin/com/igorwojda/tree/classic/levelwidth Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change 1
1
package com.igorwojda.tree.classic.levelwidth
2
2
3
3
private object Solution1 {
4
+ private fun levelWidth (tree : Node ): List <Int > {
5
+ val nodesAtLevel = mutableListOf<Int >()
6
+
7
+ var levelNodes = listOf (tree)
8
+
9
+ while (levelNodes.isNotEmpty()) {
10
+ nodesAtLevel.add(levelNodes.size)
11
+ levelNodes = levelNodes.flatMap { it.children }
12
+ }
13
+
14
+ return nodesAtLevel
15
+ }
16
+
17
+ private class Node (val data : String , val children : MutableList <Node > = mutableListOf()) {
18
+ fun add (data : String ) {
19
+ add(Node (data))
20
+ }
21
+
22
+ fun add (node : Node ) {
23
+ children.add(node)
24
+ }
25
+
26
+ fun remove (data : String ) {
27
+ children.removeAll { it.data == data }
28
+ }
29
+ }
30
+ }
31
+
32
+ private object Solution2 {
4
33
private fun levelWidth (tree : Node ): List <Int > {
5
34
val result = mutableListOf<Int >()
6
35
@@ -41,7 +70,7 @@ private object Solution1 {
41
70
}
42
71
}
43
72
44
- private object Solution2 {
73
+ private object Solution3 {
45
74
// Counters array solution
46
75
private fun levelWidth (tree : Node ): List <Int > {
47
76
val rowSeparator = null
You can’t perform that action at this time.
0 commit comments