The great tree-list recursion problem

Screen Shot 2015-02-09 at 12.56.51 AM

The great tree-list recursion problem. A binary search tree and a circular doubly linked list are conceptually built from the same type of nodes – a data field and two references to other nodes.

Given a binary search tree, rearrange the references so that it becomes a circular doubly-linked list (in sorted order). Nick Parlante describes this as one of the neatest recursive pointer problems ever devised.

Hint: create a circularly linked list A from the left subtree, a circularly linked list B from the right subtree, and make the root a one-node circularly linked list. Then merge the three lists.

Screen Shot 2015-02-22 at 1.50.19 PM