Binary tree to singly and doubly linked list
Given a binary tree, flatten it in-place to a singly linked list in preorder fashion. e.g.
1 1 / \ \ 2 5 2 / \ \ \ 3 4 6 3 \ 4 \ 5 \ 6
A single traversal of the binary tree is sufficient if paired with a next
pointer to remember the next element that will have to become the right one
Given a binary tree, convert it to a doubly linked list in-place in an in-order fashion. e.g.
10 / \ 12 15 / \ / 25 30 36 25 <-> 12 <-> 30 <-> 10 <-> 36 <-> 15
The algorithm is similar to the previous one: it is traversed left
first and a prev
pointer is kept. An additional way to return the new list head is needed since it no longer matches the root of the tree.
Both algorithms are \( O(N) \).