Saturday, November 10, 2012

Convert a Binary Tree to Linked List

Given a binary tree (or Binary Search Tree), convert it to a doubly-linked list. Can you do it inline?

Input:
                    60
           50             70
      40       55   65      75

Output:
      60 -> 50 -> 70 ->40 -> 55 -> 65 -> 75

Approach
The structure of a Binary Tree Node is similar to Linked List Node. Binary Tree node has left and right. A linked list has prev and next.
Do a Bread-First-Search (BFS) and adjust the pointers as the nodes are read.

Solution

No comments:

Post a Comment

UA-36403895-1