• + 0 comments

    yeah but technically in your second example it should be 8-4-2-1-3. I am scratching my head for a very long time , they should really add more explanation

    void topView(node * root,int fact=0,int lcomp=1,int rcomp=-1) {
        
        if(root==nullptr)
            return;
        
        cerr<<root->data<<": "<<lcomp<<" ' "<<fact<<" ' "<<rcomp<<endl;
        if((fact>rcomp) || (fact<lcomp)){
            cout<<root->data<<" ";
            if(lcomp>rcomp){
                rcomp++;
                lcomp--;
            }
            else if(fact>rcomp)
                rcomp++;
            else if(fact<lcomp)
                lcomp--;
            
            
            
        }
        topView(root->left,fact-1,lcomp,rcomp);
        topView(root->right,fact+1,lcomp,rcomp);
    }