-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseChildren.c
46 lines (41 loc) · 961 Bytes
/
reverseChildren.c
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
#include <stdio.h>
#include <stdlib.h>
typedef struct elementT {
struct elementT *left, *right;
int data;
} element;
void reverseChildren(element *root) {
if(!root)
return;
element *t = root->left;
root->left = root->right;
root->right = t;
reverseChildren(root->left);
reverseChildren(root->right);
}
element *makeNode(int d) {
element *ele = (element *)malloc(sizeof(element));
ele->data = d;
return ele;
}
void preorder(element *root) {
if(!root)
return;
printf("%d\n", root->data);
preorder(root->left);
preorder(root->right);
}
main()
{
element *root = makeNode(50);
root->left = makeNode(25);
root->right = makeNode(75);
root->left->left = makeNode(12);
root->left->right = makeNode(30);
root->left->left->left = makeNode(4);
root->right->right = makeNode(80);
preorder(root);
printf("#########\n");
reverseChildren(root);
preorder(root);
}