-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_nxt_ryt_ptrs.cpp
53 lines (38 loc) · 1.11 KB
/
populate_nxt_ryt_ptrs.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
#include <bits/stdc++.h>
using namespace std;
#define tn TreeLinkNode *
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
void connect (tn A) {
tn s = A;
tn pre = NULL;
tn l_pre = s;
while(1){
tn nl_pre = NULL;
pre = NULL;
// cout << l_pre->val << " ";
while(l_pre){
if(l_pre->left){
if(pre == NULL) pre = l_pre->left;
else pre->next = l_pre->left, pre = pre->next;
if(!nl_pre) nl_pre = l_pre->left;
}
if(l_pre->right){
if(pre == NULL) pre = l_pre->right;
else pre->next = l_pre->right, pre = pre->next;
if(!nl_pre) nl_pre = l_pre->right;
}
l_pre = l_pre->next;
}
// if(nl_pre) cout << nl_pre->val << " ";
// else cout << "NULL ";
l_pre = nl_pre;
// cout << "\n";
if(l_pre == NULL) return;
}
// cout << "\n\n";
return;
}