-
Notifications
You must be signed in to change notification settings - Fork 336
/
Binary-Tree-Traversals
53 lines (39 loc) · 1.06 KB
/
Binary-Tree-Traversals
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
/* Tree Node Structure */
struct Node {
int data;
Node* left;
Node* right;
};
/* Inorder Traversal of Binary tree */
void inorderTraversal(Node* root) {
if (root == NULL)
return;
/* recurring on left child */
inorderTraversal(root->left);
/* printing the data of node */
cout << root->data << " ";
/* recurring on right child */
inorderTraversal(root->right);
}
/* Preorder Traversal of Binary tree */
void preorderTraversal(Node* root) {
if (root == NULL)
return;
/* printing the data of node */
cout << root->data << " ";
/* recurring on left child */
preorderTraversal(root->left);
/* recurring on right child */
preorderTraversal(root->right);
}
/* Postorder Traversal of Binary tree */
void postorderTraversal(Node* root) {
if (root == NULL)
return;
/* recurring on left child */
postorderTraversal(root->left);
/* recurring on right child */
postorderTraversal(root->right);
/* printing the data of node */
cout << root->data << " ";
}