-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRecursivePreorder.c
53 lines (47 loc) · 991 Bytes
/
RecursivePreorder.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
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
struct Node* ROOT = NULL;
void preorder(struct Node* current)
{
if(current==NULL)
{
return;
}
printf("%d ",current->data);
preorder(current->left);
preorder(current->right);
}
int main()
{
//building a tree
struct Node* a = newNode(1);
struct Node* b = newNode(2);
struct Node* c = newNode(3);
struct Node* d = newNode(4);
struct Node* e = newNode(5);
struct Node* f = newNode(6);
struct Node* g = newNode(7);
ROOT = a;
ROOT->left = b;
ROOT->right = c;
ROOT->left->left = d;
ROOT->left->right=e;
ROOT->right->left=f;
ROOT->right->right=g;
preorder(ROOT);
printf("\n");
}