forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
53 lines (51 loc) · 1.35 KB
/
solution.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
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution
{
public:
void broadSearch(TreeLinkNode* queueS)
{
if(queueS == NULL)
return;
TreeLinkNode* newQueueH = NULL;
TreeLinkNode* newQueueT = NULL;
while(queueS)
{
if(queueS->left)
{
if(newQueueH == NULL)
newQueueH = queueS->left;
if(newQueueT == NULL)
newQueueT = queueS->left;
else{
newQueueT->next = queueS->left;
newQueueT = queueS->left;
}
}
if(queueS->right)
{
if(newQueueH == NULL)
newQueueH = queueS->right;
if(newQueueT == NULL)
newQueueT = queueS->right;
else{
newQueueT->next = queueS->right;
newQueueT = queueS->right;
}
}
queueS = queueS->next;
}
broadSearch(newQueueH );
}
void connect(TreeLinkNode *root)
{
TreeLinkNode* queueS = root;
broadSearch(queueS);
}
};