-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST_CreationSelf4.cpp
56 lines (56 loc) · 1003 Bytes
/
BST_CreationSelf4.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
#include<iostream>
using namespace std;
struct node
{
int data;
node *left;
node *right;
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
node *insertBST(node *root,int val)
{
if(root == NULL)
{
return new node(val);
}
if(val<root->data)
{
root->left = insertBST(root->left,val);
}
else
{
root->right = insertBST(root->right,val);
}
return root;
}
void postorder(node *root)
{
if(root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
int main()
{
node *root = NULL;
root = insertBST(root,5);
insertBST(root,2);
insertBST(root,1);
insertBST(root,3);
insertBST(root,4);
insertBST(root,8);
insertBST(root,9);
insertBST(root,6);
insertBST(root,7);
insertBST(root,10);
postorder(root);
return 0;
}