-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#116.cc
More file actions
33 lines (32 loc) · 955 Bytes
/
Copy pathLeetCode#116.cc
File metadata and controls
33 lines (32 loc) · 955 Bytes
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
/**
* 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 connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
TreeLinkNode* Next = NULL;
TreeLinkNode* Prev = NULL;
while(root){
for(;root;root=root->next){
if(!Next) Next = root->left ? root->left:root->right;
if(root->left){
if(Prev) Prev->next = root->left;
Prev = root->left;
}
if(root->right){
if(Prev) Prev->next = root->right;
Prev = root->right;
}
}
root=Next;
Prev=Next = NULL;
}
}
};