-
Notifications
You must be signed in to change notification settings - Fork 127
/
Tree_traversal.cpp
62 lines (49 loc) · 1010 Bytes
/
Tree_traversal.cpp
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
62
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node *newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void Preorder(struct Node *node)
{
if (node == NULL)
return;
cout << node->data << " ";
Preorder(node->left);
Preorder(node->right);
}
void Postorder(struct Node* node)
{
if (node == NULL)
return;
Postorder(node->left);
Postorder(node->right);
cout << node->data << " ";
}
void Inorder(struct Node* node)
{
if (node == NULL)
return;
Inorder(node->left);
cout << node->data << " ";
Inorder(node->right);
}
int main()
{
struct Node *root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
cout << "\nPreorder traversal of binary tree is \n";
Preorder(root);
return 0;
}