Bottom View of a Binary Tree

PROBLEM :

Given a Binary Tree,  print the bottom view from left to right. A node x is there in output if x is the bottommost node at its horizontal distance from root. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.

Examples:

                      20
                    /    \
                  8       22
                /   \        \
              5      3       25
                    /   \    
                  10    14

For the above tree the output should be 5, 10, 3, 14, 25.

If there are multiple bottom-most nodes for a horizontal distance from root, then print the later one in level traversal. For example, in the below diagram, 3 and 4 are both the bottom-most nodes at horizontal distance 0, we need to print 4.

                      20
                    /    \
                  8       22
                /   \     /   \
              5      3 4     25
                     /    \    
                 10       14

For the above tree the output should be 5, 10, 4, 14, 25.

Input:
The task is to complete the method which takes one argument, root of Binary Tree. The struct Node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.

Output:
The function should print nodes in bottom view of Binary Tree.  Your code should not print a newline, it is added by the caller code that runs your function.

Constraints:
1 <=T<= 30
0 <= Number of nodes<= 100
0 <= Data of a node<= 1000

Example:
Input:
2
2
1 2 R 1 3 L
4
10 20 L 10 30 R 20 40 L 20 60 R

Output:
3 1 2
40 20 60 30

There are two test casess.  First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.   Second test case represents a tree with 4 edges and 5 nodes.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/* Tree node class
struct Node
{
    int data;
    Node *left, *right;
}; */

// Method that prints the bottom view.

void find_Min_Max(Node *, int *,int *,int ) ;
void print_top(Node *,int ,bool *,int ) ;
void bottomView(Node *root)
{
   if(root==NULL)
         return ;
    bool state=true ;
    int min,max,i ;
    min=0 ;
    max=0 ;
   
    find_Min_Max(root,&min,&max,0) ;
   
    for(i=min;i<=max;i++)
    {
        print_top(root,i,&state,0) ;
        state=true ;
    }
}


void find_Min_Max(Node *root, int *min,int *max,int curr)
{
    if(root==NULL)
          return ;
   
    if(curr>(*max))
        (*max)=curr ;
    if(curr<(*min))
        (*min)=curr ;
   
    find_Min_Max(root->left,&(*min),&(*max),curr-1) ;
    find_Min_Max(root->right,&(*min),&(*max),curr+1) ;
}

void print_top(Node *root,int dest,bool *state,int curr)
{
    if(root==NULL)
        return ;
       
    if(dest<0)
    {
        print_top(root->left,dest,&(*state),curr-1) ;
        print_top(root->right,dest,&(*state),curr+1) ;  
    }
    else
    {
        print_top(root->right,dest,&(*state),curr+1) ;
        print_top(root->left,dest,&(*state),curr-1) ;
    }
   
    if((*state)!=true)
            return ;
       
    if(curr==dest&&(*state)==true)
    {
        cout<<root->data<<" " ;
        (*state)=false ;
    }
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )