-
Notifications
You must be signed in to change notification settings - Fork 0
/
117 Populating Next Right Pointers in Each Node II.c
124 lines (103 loc) · 2.49 KB
/
117 Populating Next Right Pointers in Each Node II.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
/**
* Definition for a Node.
* struct Node {
* int val;
* struct Node *left;
* struct Node *right;
* struct Node *next;
* };
*/
#define MAX_NODES 6000
typedef struct
{
struct Node * queue_arr[MAX_NODES];
int level_arr[MAX_NODES];
int rear;
int front;
}Queue;
int isQueueEmpty(Queue * tmp_queue)
{
if(tmp_queue->front == -1)
return 1;
else
return 0;
}
int isQueueFull(Queue * tmp_queue)
{
if(tmp_queue->rear == MAX_NODES - 1)
return 1;
else
return 0;
}
void enqueue(Queue * tmp_queue, struct Node * element, int level)
{
if(isQueueFull(tmp_queue))
return;
tmp_queue->queue_arr[++(tmp_queue->rear)] = element;
tmp_queue->level_arr[tmp_queue->rear] = level;
if(tmp_queue->front == -1)
tmp_queue->front = 0;
return;
}
struct Node * dequeue(Queue * tmp_queue, int * level)
{
if(isQueueEmpty(tmp_queue))
return NULL;
struct Node * ret_node = tmp_queue->queue_arr[tmp_queue->front];
*level = tmp_queue->level_arr[tmp_queue->front];
if(tmp_queue->front == tmp_queue->rear)
{
tmp_queue->front = -1;
tmp_queue->rear = -1;
}
else
{
(tmp_queue->front)++;
}
return ret_node;
}
struct Node * peek(Queue * tmp_queue, int * level)
{
if(tmp_queue->front == tmp_queue->rear)
{
return NULL;
}
else
{
struct Node * ret_ptr = tmp_queue->queue_arr[tmp_queue->front + 1];
*level = tmp_queue->level_arr[tmp_queue->front + 1];
return ret_ptr;
}
return NULL;
}
struct Node* connect(struct Node* root)
{
if(root == NULL)
return root;
Queue tmp_queue;
tmp_queue.front = -1;
tmp_queue.rear = -1;
int cur_level = 1;
int prev_level = 1;
struct Node * trav_ptr = root;
enqueue(&tmp_queue, trav_ptr, cur_level);
while(!isQueueEmpty(&tmp_queue))
{
int peek_level;
struct Node * peek_node;
peek_node = peek(&tmp_queue, &peek_level);
trav_ptr = dequeue(&tmp_queue, &cur_level);
if(peek_node != NULL)
{
if(peek_level == cur_level)
{
trav_ptr->next = peek_node;
}
}
if(trav_ptr->left != NULL)
enqueue(&tmp_queue, trav_ptr->left, cur_level+1);
if(trav_ptr->right != NULL)
enqueue(&tmp_queue, trav_ptr->right, cur_level+1);
}
return root;
}