-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstLevelLists.js
58 lines (57 loc) · 1.11 KB
/
bstLevelLists.js
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
class Node {
constructor(val) {
this.val = val
this.left = null
this.right = null
}
}
class BST {
constructor(node) {
this.root = node
}
insert(val) {
this.root = this._insertRec(this.root, val)
}
_insertRec(root, val) {
if(!root) {
return new Node(val)
}
if(val > root.val)
root.right = this._insertRec(root.right, val)
else
root.left = this._insertRec(root.left, val)
return root
}
createLevelLists() {
if(!this.root)
return []
const res = []
let current = []
let parents = []
current.push(this.root)
while(current.length > 0) {
res.push(current)
parents = current
current = []
for(let parent of parents) {
if(parent.left)
current.push(parent.left)
if(parent.right)
current.push(parent.right)
}
}
return res.map(arr => arr.map(i => i.val))
}
}
let bst = new BST(new Node(5))
bst.insert(3)
bst.insert(8)
bst.insert(2)
bst.insert(4)
bst.insert(1)
bst.insert(7)
bst.insert(6)
bst.insert(9)
bst.insert(10)
bst.insert(11)
console.log(bst.createLevelLists())