-
Notifications
You must be signed in to change notification settings - Fork 80
/
SwapNodes.cpp
90 lines (81 loc) · 1.6 KB
/
SwapNodes.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int data;
struct node *lChild;
struct node *rChild;
};
void swap(struct node *x)
{
if(x==NULL)
return;
struct node *temp;
temp=x->rChild;
x->rChild=x->lChild;
x->lChild=temp;
}
void swapAtD(struct node *start,int h,int d)
{
if(start==NULL)
return;
if(h%d==0)
{
swap(start);
}
swapAtD(start->lChild,h+1,d);
swapAtD(start->rChild,h+1,d);
}
struct node* addNode(int i)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->lChild=NULL;
temp->rChild=NULL;
temp->data=i;
return temp;
}
void inorder(struct node *start)
{
if(start==NULL)
return;
inorder(start->lChild);
cout<<start->data<<" ";
inorder(start->rChild);
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,l,r;
struct node* ptrArray[1025]={NULL};
cin>>n;
for(int i=1;i<=n;i++)
{
if(ptrArray[i]==NULL)
ptrArray[i]=addNode(i);
cin>>l>>r;
if(l!=-1)
{
ptrArray[l]=addNode(l);
ptrArray[i]->lChild=ptrArray[l];
}
if(r!=-1)
{
ptrArray[r]=addNode(r);
ptrArray[i]->rChild=ptrArray[r];
}
}
int T,d;
cin>>T;
while(T--)
{
cin>>d;
swapAtD(ptrArray[1],1,d);
inorder(ptrArray[1]);
cout<<endl;
}
return 0;
}