-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
61 lines (51 loc) · 1.32 KB
/
main.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
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/*
The binary tree in this example is shown as follow
1
/ \
2 3
/ \ /
4 5 6
/
7
pre-order traversal: 1 2 4 5 7 3 6
*/
let output = [] /* store the sequence of output */
function preorderTraversalByRecursion(root) {
if (root == null) return;
output.push(root.val);
preorderTraversalByRecursion(root.left);
preorderTraversalByRecursion(root.right);
}
function preorderTraversal(root) {
if (root == null) return;
const nodeStack = [];
let node = root;
while (node != null || nodeStack.length != 0) {
while (node != null) {
output.push(node.val);
nodeStack.push(node.right);
node = node.left;
}
node = nodeStack.pop();
}
}
function generateBTree(nodes, index) {
let node = null;
if (index < nodes.length && nodes[index] != null) {
node = new TreeNode(nodes[index]);
node.left = generateBTree(nodes, index * 2 + 1);
node.right = generateBTree(nodes, index * 2 + 2)
}
return node
}
const nodes = [1, 2, 3, 4, 5, 6, null, null, null, 7]
const root = generateBTree(nodes, 0);
preorderTraversalByRecursion(root)
console.log(output);
output = [];
preorderTraversal(root)
console.log(output);