-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.test.ts
50 lines (42 loc) · 993 Bytes
/
16.test.ts
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
import { assertEquals } from 'assert'
function transformTree(tree: (number | null)[]) {
const nodes: (Record<string, any> | null)[] = []
let index = 0
for (const value of tree) {
const side = ['right', 'left'][index % 2]
const node = { value, left: null, right: null }
const child = [null, { [side]: node }][+(value !== null)]
const parent = nodes[index - Math.floor(index / 2) - 1]
Object.assign(parent ?? {}, child)
nodes.push(node)
index++
}
return nodes[0] ?? null
}
Deno.test('Reto #16: ❌ Despliegue en viernes', () => {
assertEquals(transformTree([3, 1, 0, 8, 12, null, 1]), {
value: 3,
left: {
value: 1,
left: {
value: 8,
left: null,
right: null,
},
right: {
value: 12,
left: null,
right: null,
},
},
right: {
value: 0,
left: null,
right: {
value: 1,
left: null,
right: null,
},
},
})
})