forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Preorder_Traversal.ts
More file actions
53 lines (44 loc) · 921 Bytes
/
Tree_Preorder_Traversal.ts
File metadata and controls
53 lines (44 loc) · 921 Bytes
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
/* Implementation of the preorder traversal of a binary tree in TypeScript language */
// Declaring the node class
class node {
right: any;
value: any;
left: any;
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
// Recursive Function for Preorder Tree Transversal.
function PreOrder(root)
{
if (root) {
console.log(root.value);
PreOrder(root.left);
PreOrder(root.right);
}
}
// Sample Input
var root = new node(1);
root.left = new node(2);
root.right = new node(3);
root.left.right = new node(4);
root.right.left = new node(8);
root.right.right = new node(7);
root.left.right.left = new node(5);
root.left.right.left.right = new node(6);
// Sample Output
console.log("Preorder traversal of tree formed is:-");
PreOrder(root);
/* OUTPUT:
Preorder traversal of tree formed is:-
1
2
4
5
6
3
8
7
*/