0421-0430

421. Maximum XOR of Two Numbers in an Array $\star\star$

422. Valid Word Square $\star$

423. Reconstruct Original Digits from English $\star\star$

424. Longest Repeating Character Replacement $\star\star$

425. Word Squares $\star\star\star$

426. Convert Binary Search Tree to Sorted Doubly Linked List $\star\star$

427. Construct Quad Tree $\star\star$

428. Serialize and Deserialize N-ary Tree $\star\star\star$

429. N-ary Tree Level Order Traversal $\star\star$

430. Flatten a Multilevel Doubly Linked List $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  Node* flatten(Node* head) {
    for (Node* curr = head; curr; curr = curr->next)
      if (curr->child) {
        Node* next = curr->next;
        curr->next = curr->child;
        curr->next->prev = curr;
        curr->child = NULL;
        Node* tail = curr->next;
        while (tail->next) tail = tail->next;
        tail->next = next;
        if (next) next->prev = tail;
      }

    return head;
  }
};