-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Using_array.cpp
More file actions
49 lines (46 loc) · 948 Bytes
/
Copy pathTree_Using_array.cpp
File metadata and controls
49 lines (46 loc) · 948 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
#include<bits/stdc++.h>
using namespace std;
int root=0;
void preorder(int arr[],int root,int n)
{
if(root>=n)
return;
cout<<arr[root]<<" ";
preorder(arr,root*2+1,n);
preorder(arr,root*2+2,n);
}
void postorder(int arr[],int root,int n)
{
if(root>=n)
return ;
postorder(arr,root*2+1,n);
postorder(arr,root*2+2,n);
cout<<arr[root]<<" ";
}
void inorder(int arr[],int root,int n)
{
if(root>=n)
return ;
inorder(arr,root*2+1,n);
cout<<arr[root]<<" ";
inorder(arr,root*2+2,n);
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Preorder traversal is :";
preorder(arr,root,n);
cout<<endl;
cout<<"Postorder traversal is :";
postorder(arr,root,n);
cout<<endl;
cout<<"Inorder traversal is :";
inorder(arr,root,n);
cout<<endl;
}