Showing posts with label BFS. Show all posts
Showing posts with label BFS. Show all posts

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



Tuesday, October 23, 2012

Binary Tree - Microsoft Interview Questions

This one contains 3 different problems on a complete Binary Tree.
1. Create and Print a Binary Tree in the same input order.
2. Print the tree in a zig-zag fashion. Even levels right to left and Odd levels left to right
3. Take a snapshot of the tree and put it on a co-ordinate system with left-most node as (0,0)

Solution



UA-36403895-1