Skip to content

Commit bdd315b

Browse files
committed
new solutions of "Populating Next Right Pointers in Each Node"
1) level order traversal by a explicit queue 2) constant space, use `next` pointer to represent the queue
1 parent f874fde commit bdd315b

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

‎algorithms/cpp/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.II.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,72 @@ void connect1(TreeLinkNode *root) {
122122
v.pop_back();
123123
}
124124
}
125+
126+
void connect3(TreeLinkNode *root) {
127+
if(root == NULL) return;
128+
129+
queue<TreeLinkNode*> q;
130+
TreeLinkNode *prev, *last;
131+
prev = last = root;
132+
133+
q.push(root);
134+
while(!q.empty()) {
135+
TreeLinkNode* p = q.front();
136+
q.pop();
137+
138+
prev->next = p;
139+
if(p->left ) q.push(p->left);
140+
if(p->right) q.push(p->right);
141+
142+
if(p == last) { // meets last of current level
143+
// now, q contains all nodes of next level
144+
last = q.back();
145+
p->next = NULL; // cut down.
146+
prev = q.front();
147+
}
148+
else {
149+
prev = p;
150+
}
151+
}
152+
}
153+
154+
// constant space
155+
// key point: we can use `next` pointer to represent
156+
// the buffering queue of level order traversal.
157+
void connect4(TreeLinkNode *root) {
158+
if(root == NULL) return;
159+
160+
TreeLinkNode *head, *tail;
161+
TreeLinkNode *prev, *last;
162+
163+
head = tail = NULL;
164+
prev = last = root;
165+
166+
#define push(p) \
167+
if(head == NULL) { head = tail = p; } \
168+
else { tail->next = p; tail = p; }
169+
170+
push(root);
171+
while(head != NULL) {
172+
TreeLinkNode* p = head;
173+
head = head->next; // pop
174+
175+
prev->next = p;
176+
if(p->left ) push(p->left);
177+
if(p->right) push(p->right);
178+
179+
if(p == last) {
180+
last = tail;
181+
p->next = NULL; // cut down.
182+
prev = head;
183+
}
184+
else {
185+
prev = p;
186+
}
187+
}
188+
#undef push
189+
}
190+
125191
void printTree(TreeLinkNode *root){
126192
if (root == NULL){
127193
return;

‎algorithms/cpp/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include <stdio.h>
4646
#include <vector>
47+
#include <queue>
4748
using namespace std;
4849

4950
/**
@@ -130,6 +131,71 @@ void connect2(TreeLinkNode *root) {
130131
}
131132
}
132133

134+
void connect3(TreeLinkNode *root) {
135+
if(root == NULL) return;
136+
137+
queue<TreeLinkNode*> q;
138+
TreeLinkNode *prev, *last;
139+
prev = last = root;
140+
141+
q.push(root);
142+
while(!q.empty()) {
143+
TreeLinkNode* p = q.front();
144+
q.pop();
145+
146+
prev->next = p;
147+
if(p->left ) q.push(p->left);
148+
if(p->right) q.push(p->right);
149+
150+
if(p == last) { // meets last of current level
151+
// now, q contains all nodes of next level
152+
last = q.back();
153+
p->next = NULL; // cut down.
154+
prev = q.front();
155+
}
156+
else {
157+
prev = p;
158+
}
159+
}
160+
}
161+
162+
// constant space
163+
// key point: we can use `next` pointer to represent
164+
// the buffering queue of level order traversal.
165+
void connect4(TreeLinkNode *root) {
166+
if(root == NULL) return;
167+
168+
TreeLinkNode *head, *tail;
169+
TreeLinkNode *prev, *last;
170+
171+
head = tail = NULL;
172+
prev = last = root;
173+
174+
#define push(p) \
175+
if(head == NULL) { head = tail = p; } \
176+
else { tail->next = p; tail = p; }
177+
178+
push(root);
179+
while(head != NULL) {
180+
TreeLinkNode* p = head;
181+
head = head->next; // pop
182+
183+
prev->next = p;
184+
if(p->left ) push(p->left);
185+
if(p->right) push(p->right);
186+
187+
if(p == last) {
188+
last = tail;
189+
p->next = NULL; // cut down.
190+
prev = head;
191+
}
192+
else {
193+
prev = p;
194+
}
195+
}
196+
#undef push
197+
}
198+
133199
void printTree(TreeLinkNode *root){
134200
if (root == NULL){
135201
return;

0 commit comments

Comments
 (0)