-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnAryTreeLevelOrderTraversal.js
66 lines (56 loc) · 1.46 KB
/
nAryTreeLevelOrderTraversal.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
59
60
61
62
63
64
65
66
class Node {
constructor(val, children) {
this.val = val;
this.children = children;
}
}
const levelOrder = root => {
const output = [];
if (!root) return output;
let queue = root.children;
output.push([root.val])
while (queue.length) {
const level = [];
for (let i = 0; i < queue.length; i++) {
level.push(queue[i].val);
queue.push(...queue[i].children);
}
output.push(level);
}
return output;
};
// specification
// input: n-ary level tree
// output: array of arrays representing each level of the tree
// constraints:
// edge cases:
// justification
// to print out the nodes at each level of the tree
// explanation
// the structure of the input n-ary tree will determine the output array of arrays that represent the values at each level
// visualization
// approximation
// declare empty output array
// if there is no root, return output
// initialize queue with root in it
// while the queuehas elements
// declare empty array for current level
// add all node values from current level
// add each node to the queue
// return output
// verification
// implementation
const aRoot = new Node(1, []);
const aLeft = new Node(3, []);
const aMiddle = new Node(2, []);
const aRight = new Node(4, []);
aRoot.children.push(aLeft, aMiddle, aRight);
aLeftLeft = new Node(5, []);
aLeftRight = new Node(6, []);
aLeft.children.push(aLeftLeft, aLeftRight);
const expected = [
[1],
[3,2,4],
[5,6]
]
console.log(levelOrder(aRoot));