-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Using_Linked_List.cpp
More file actions
64 lines (62 loc) · 1.22 KB
/
Copy pathTree_Using_Linked_List.cpp
File metadata and controls
64 lines (62 loc) · 1.22 KB
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
63
64
#include<bits/stdc++.h>
using namespace std;
//this tree has been made by taking user input
struct node
{
int data;
node* left;
node*right;
};
node*create_node(int data)
{
node*bst=new node;
bst->data=data;
bst->right=NULL;
bst->left=NULL;
return bst;
}
node*build(node*root)
{
cout<<"enter the item :"<<endl;
int item;
cin>>item;
root=create_node(item);
if(item==-1)
{
return NULL;
}
cout<<"insert data in the left of "<<item<<endl;
root->left=build(root->left);
cout<<"inset data in the right of "<<item<<endl;
root->right=build(root->right);
return root;
}
void pre_order(node*root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
pre_order(root->left);
pre_order(root->right);
}
void inorder(node*root)
{
if(root==NULL)
{
return;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
//2 7 1 -1 -1 6 5 -1 -1 10 -1 -1 9 -1 8 3 -1 -1 4 -1 -1
int main()
{
node*root=NULL;
root=build(root);
cout<<"preorder traversal is ";
pre_order(root);
cout<<endl;
cout<<"inorder traversal is ";
inorder(root);
}