-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_traversal_rec.js
48 lines (41 loc) · 1018 Bytes
/
tree_traversal_rec.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
import { root } from './tree';
`
1
/ \
2 3
/ \ / \
4 5 6 7
`;
function inOrderTraversal(root) {
`
left -> root -> right
4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7
`;
if (root.left) inOrderTraversal(root.left);
console.log(root.val);
if (root.right) inOrderTraversal(root.right);
}
console.log('In order traversal');
inOrderTraversal(root);
function preOrderTraversal(root) {
`
root -> left -> right
1 -> 2 -> 4 -> 5 -> 3 -> 6 -> 7
`;
console.log(root.val);
if (root.left) preOrderTraversal(root.left);
if (root.right) preOrderTraversal(root.right);
}
console.log('Pre order traversal');
preOrderTraversal(root);
function postOrderTraversal(root) {
`
root -> left -> right
4 -> 5 -> 2 -> 6 -> 7 -> 3 -> 1
`;
if (root.left) postOrderTraversal(root.left);
if (root.right) postOrderTraversal(root.right);
console.log(root.val);
}
console.log('Post order traversal');
postOrderTraversal(root);