-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_117_PopulatingNextRightPointersInEachNodeII.cpp
178 lines (152 loc) · 4.48 KB
/
LC_117_PopulatingNextRightPointersInEachNodeII.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
117. Populating Next Right Pointers in Each Node II
https://practice.geeksforgeeks.org/problems/connect-nodes-at-same-level/1
*/
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
if(!root)
return root;
// return connect_BFS(root);
// return connect_Rec(root);
Node* curLevel = root;
Node* nxtfirstNode, *prev, *ptr;
while(curLevel)
{
ptr = curLevel; // for traversing in the current level
prev = nxtfirstNode = nullptr;
while(ptr) // travel that particular level
{
if(ptr->left)
{
if(!nxtfirstNode)
nxtfirstNode = ptr->left;
else
prev->next = ptr->left;
prev = ptr->left;
}
if(ptr->right)
{
if(!nxtfirstNode)
nxtfirstNode = ptr->right;
else
prev->next = ptr->right;
prev = ptr->right;
}
ptr = ptr->next;
}//while ptr
curLevel = nxtfirstNode;
}
return root;
}
// DoestNot Work
// Node* connect_Rec(Node* root) {
// if(!root)
// return root;
// if(root && root->left) // cur and next level exist;
// {
// root->left->next = root->right;
// connect_Rec(root->left);
// if(root->next)
// {
// if(root->next->left)
// root->right->next = root->next->left;
// else
// root->right->next = root->next->right;
// }
// connect_Rec(root->right);
// }
// return root;
// }
Node* connect_BFS(Node* root) {
queue<Node*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
Node* prev = nullptr;
while(size--)
{
Node* curNode = q.front(); q.pop();
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
if(prev)
prev->next = curNode;
prev = curNode;
}
prev->next = nullptr;
}
return root;
}
};
//GFG ---------------------------------------------
class Solution
{
public:
//Function to connect nodes at same level.
void connect(Node *root)
{
// root=connectBFS(root);
connectDFS(root);
}
Node* getRight(Node* node)
{
while(node)
{
if(node->left)
return node->left;
if(node->right)
return node->right;
node = node->nextRight;
}
return node;
}
void connectDFS(Node* root)
{
if(!root) return;
Node* rootKaNextRight = getRight(root->nextRight);
if(root->left)
root->left->nextRight = root->right? root->right : rootKaNextRight;
if(root->right)
root->right->nextRight = rootKaNextRight;
connectDFS(root->left);
connectDFS(root->right);
}
Node* connectBFS(Node* root)
{
if(!root) return nullptr;
queue<Node*> q; q.push(root);
while(!q.empty())
{
int sz = q.size();
Node* prev = nullptr;
while(sz--)
{
Node* cur = q.front(); q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(prev) prev->nextRight = cur;
prev = cur;
}
prev->nextRight = nullptr;
}
return root;
}
};