-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathprintNthLevel_binaryTree.c
143 lines (126 loc) · 2.9 KB
/
printNthLevel_binaryTree.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Given a binary tree and an integer n, print the nodes at the nth level
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
// Method 1 - Using Recursion
void printLevel(struct Node *root, int level)
{
if (root == NULL)
return;
if (level == 1)
printf("%d ", root->data);
else
{
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
}
// Method 2 - Using level order traversal
// ===== Queue and its functions ===== //
const int qSize = 100;
struct Node *q[100];
int front = -1, rear = -1;
void qPush(struct Node *);
struct Node *qPop();
bool qEmpty();
// In this approach, we can check if the current level is the required level. If yes, then print it.
// Time complexity: O(n)
void printLevel2(struct Node *root, int targetLevel)
{
if (root == NULL)
return;
qPush(root);
struct Node *curr;
int level = 1;
while (!qEmpty())
{
int size = rear - front + 1;
for (int i = 0; i < size; i++)
{
curr = qPop();
if (level == targetLevel)
printf("%d ", curr->data);
if (curr->left)
qPush(curr->left);
if (curr->right)
qPush(curr->right);
}
level++;
if (level > targetLevel)
break;
}
}
struct Node *createNode(int);
struct Node *insertLeft(struct Node *, int);
struct Node *insertRight(struct Node *, int);
int main()
{
struct Node *root = NULL;
root = createNode(7);
insertLeft(root, 5);
insertRight(root, 10);
insertLeft(root->left, 2);
insertRight(root->left, 6);
insertLeft(root->right, 8);
insertRight(root->right, 14);
// For reference
// 7
// / \
// 5 10
// / \ / \
// 2 6 8 14
int level;
printf("\nEnter level\n");
scanf("%d", &level);
printf("\nNodes at level %d\n", level);
printLevel(root, level);
return 0;
}
// ====== Queue functions ====== //
void qPush(struct Node *node)
{
if (rear == qSize - 1)
return;
if (front == -1)
++front;
q[++rear] = node;
}
struct Node *qPop()
{
if (qEmpty())
return NULL;
struct Node *deleted = q[front];
if (rear == front)
front = rear = -1;
else
front++;
return deleted;
}
bool qEmpty()
{
return front == -1 && rear == -1;
}
struct Node *createNode(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node *insertLeft(struct Node *root, int data)
{
root->left = createNode(data);
return root->left;
}
struct Node *insertRight(struct Node *root, int data)
{
root->right = createNode(data);
return root->right;
}