LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接
中等
作者:
JasonSun
,
2020-08-30 07:06:40
,
所有人可见
,
阅读 447
Implicit Tree Fold with side effects
class Solution {
public:
Node* flatten(Node* head) {
std::function<void(Node*)> fold = [&, pre = (Node*)nullptr](Node* n) mutable {
if (n == nullptr) {
return;
}
else {
std::exchange(pre, n);
if (n->child != nullptr) {
fold(n->child);
if (n->next) {
pre->next = n->next;
n->next->prev = pre;
}
n->child->prev = n;
n->next = n->child;
n->child = nullptr;
}
fold(pre->next);
}
};
return fold(head), head;
}
};