-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_258.js
53 lines (47 loc) Β· 1 KB
/
problem_258.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
/**
* Definition for a binary tree node.
*
*/
function TreeNode(val) {
this.val = val;
this.left = null;
this.right = null;
}
/**
* Finds the boustrophedon order given a tree node
* @param {TreeNode} root
* @return {number[]}
*/
function boustrophedonOrder(root) {
const result = [];
const traverse = (node, depth) => {
if (node) {
if (!result[depth]) {
result[depth] = [];
}
if (depth % 2 === 0) {
result[depth].push(node.val);
} else {
result[depth].unshift(node.val);
}
traverse(node.left, depth + 1);
traverse(node.right, depth + 1);
}
};
traverse(root, 0);
return result.flat();
}
const root = new TreeNode(1);
const a = new TreeNode(2);
const b = new TreeNode(3);
const c = new TreeNode(4);
const d = new TreeNode(5);
const e = new TreeNode(6);
const f = new TreeNode(7);
root.left = a;
root.right = b;
a.left = c;
a.right = d;
b.left = e;
b.right = f;
console.log(boustrophedonOrder(root)); // [1, 3, 2, 4, 5, 6, 7]