Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package traverse_level_order

public data class TreeNode(val `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null)

public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, any reason to prefer Collection over List for the return type?

if (this == null) {
return sequenceOf()
}

var nodesAtLevel = listOf(this)

return sequence {
while (nodesAtLevel.isNotEmpty()) {
yield(nodesAtLevel)

nodesAtLevel = nodesAtLevel.flatMap { listOf(it.left, it.right) }.filterNotNull()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package traverse_level_order

import kotlin.test.*

internal class LevelOrderTraversalTest {

@Test
fun itTraversesLevelOrderEmptyTree() {
val root: TreeNode? = null
assertContentEquals(
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), emptyList<Int>())
}

@Test
fun itTraversesLevelOrderSingleNode() {
val root: TreeNode = TreeNode(0)
assertContentEquals(
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), listOf<Int>(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine <Int> for the list type is inferred from the use of 0 as the value?

}

@Test
fun itTraversesLevelOrderFullTree() {
val root: TreeNode =
TreeNode(
1,
TreeNode(2, TreeNode(4, TreeNode(8)), TreeNode(5)),
TreeNode(3, TreeNode(6), TreeNode(7)))
assertContentEquals(
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the most effective test, I think it would be helpful to do an assert on the levels here.

listOf<Int>(1, 2, 3, 4, 5, 6, 7, 8))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "traverseLevelOrder()"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,34 @@ public fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;

exports[`App can equip single goody: Kotlin traverseLevelOrder() 1`] = `
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
// Adventure Pack commit fake-commit-hash
// Running at: https://example.com/

public data class TreeNode(val \`val\`: Int, var left: TreeNode? = null, var right: TreeNode? = null)

public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
if (this == null) {
return sequenceOf()
}

var nodesAtLevel = listOf(this)

return sequence {
while (nodesAtLevel.isNotEmpty()) {
yield(nodesAtLevel)

nodesAtLevel = nodesAtLevel
.flatMap { listOf(it.left, it.right) }
.filterNotNull()
}
}
}

/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;

exports[`App can equip single goody: Python 3 UnionFind 1`] = `
"########################## BEGIN ADVENTURE PACK CODE ###########################
# Adventure Pack commit fake-commit-hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,30 @@ import gcd_int_int.gcd
public fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b"
`;

exports[`App can render goody: Kotlin traverseLevelOrder() 1`] = `
"package traverse_level_order

public data class TreeNode(val \`val\`: Int, var left: TreeNode? = null, var right: TreeNode? = null)

public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
if (this == null) {
return sequenceOf()
}

var nodesAtLevel = listOf(this)

return sequence {
while (nodesAtLevel.isNotEmpty()) {
yield(nodesAtLevel)

nodesAtLevel = nodesAtLevel
.flatMap { listOf(it.left, it.right) }
.filterNotNull()
}
}
}"
`;

exports[`App can render goody: Python 3 UnionFind 1`] = `
"class UnionFind:

Expand Down
Loading