-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path430.cpp
35 lines (35 loc) · 802 Bytes
/
430.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
class Solution {
public:
Node * solve(Node * root){
Node * curr=root;
Node * tail=root;
while(curr){
Node * child=curr->child;
Node * next=curr->next;
if(child){
Node * _tail=solve(child);
_tail->next=next;
if(next){
next->prev=_tail;
}
curr->next=child;
child->prev=curr;
curr->child=NULL;
curr=_tail;
}
else {
curr=next;
}
if(curr){
tail=curr;
}
}
return tail;
}
Node* flatten(Node* head) {
if(head){
solve(head);
}
return head;
}
};