composite recursion method
Inspired by 114. Flatten Binary Tree to Linked List, using head and tail as parameters in recursive method. \n\nclass Solution {\n    public Node flatten(Node h
Inspired by 114. Flatten Binary Tree to Linked List, using head and tail as parameters in recursive method. \n\nclass Solution {\n    public Node flatten(Node head) { \n        return flatten(head, null);\n    }\n    \n    private Node flatten(Node head, Node tail){\n        if (head == null) return tail;\n        if (head.child == null) { //no child, just convert head.next\n            Node next = flatten(head.next, tail);\n            head.next = next;\n            if (next != null) next.prev = head; \n        } else { //child is flattened and inserted between head and head.next\n            Node child = flatten(head.child, flatten(head.next, tail));\n            head.next = child;\n            if (child !=null) child.prev = head;\n            head.child = null;\n        }\n        return head;\n    }\n}
[]