We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Trees
  4. Tree : Top View
  5. Discussions

Tree : Top View

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • dennysregalado 4 years ago+ 0 comments

    We can merge these two functions in one using default value for the second argument and define 3 possible values for each funcion call: { 0: original, 1: left , 2: right}.

    void top_view(node * root, int src=0)
    {
        i f(root == NULL)
            return;
    
        if(src==0){        
            top_view(root->left, 1);        
            printf("%d ",root->data);
            top_view(root->right,2);        
        }    
        if(src==1){        
            top_view(root->left, 1);            
            printf("%d ",root->data);
        }
        if(src==2){
            printf("%d ",root->data);
            top_view(root->right,2);
        }    
    }
    
    0|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature