# C++ recursive solution, easy understanding

• `````` void connect(TreeLinkNode *root) {
if (!root) return;
for (TreeLinkNode *cur = root, *pre = &dummy; cur; cur = cur->next) {
if (cur->left) {
pre->next = cur->left;
pre = pre->next;
}
if (cur->right) {
pre->next = cur->right;
pre = pre->next;
}
}
connect(dummy.next);
}``````

• But the problem has to be solved with only constant extra space.

• I see, if in that case, you may can change the recursive call to a while loop as following

``````void connect(TreeLinkNode *root) {
if (!root) return;
TreeLinkNode dummy(INT_MIN), *cur = root, *pre = &dummy;
while (cur) {
for ( pre = &dummy; cur; cur = cur->next) {
if (cur->left) {
pre->next = cur->left;
pre = pre->next;
}
if (cur->right) {
pre->next = cur->right;
pre = pre->next;
}
}
//connect(dummy.next);
cur = dummy.next;
dummy.next = nullptr;
}
``````

}

• sorry, I cannot understand your code, given a binary tree like:

``````  1
/    \

2         3

/  \           \
``````

4 5 6

In the first for loop, dummy(pre)is pointed to 3,while dummy.next is NULL,how can we continue the next level?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.