forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdalma.kt
More file actions
61 lines (52 loc) · 1.44 KB
/
jdalma.kt
File metadata and controls
61 lines (52 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package leetcode_study
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
class `graph-valid-tree` {
/**
* TC: O(n), SC: O(n)
*/
fun validTree(nodeSize: Int, edges: Array<IntArray>): Boolean {
if (nodeSize - 1 != edges.size) {
return false
}
val visited = mutableSetOf<Int>()
val adj = List(nodeSize) { mutableListOf<Int>() }
for (e in edges) {
adj[e[0]].add(e[1])
adj[e[1]].add(e[0])
}
val queue = ArrayDeque<Int>().apply {
this.add(0)
}
while (queue.isNotEmpty()) {
val now = queue.removeFirst()
visited.add(now)
for (next in adj[now]) {
if (!visited.contains(next)) {
queue.add(next)
}
}
}
return nodeSize == visited.size
}
@Test
fun `노드가 트리의 조건을 만족하는지 여부를 반환한다`() {
validTree(5,
arrayOf(
intArrayOf(0,1),
intArrayOf(0,2),
intArrayOf(0,3),
intArrayOf(1,4)
)
) shouldBe true
validTree(5,
arrayOf(
intArrayOf(0,1),
intArrayOf(0,2),
intArrayOf(0,3),
intArrayOf(1,4),
intArrayOf(2,3),
)
) shouldBe false
}
}