Tree right view

why only one test case passes

for i/p
1 2 3 -1 -1 -1 4 -1 -1
o/p shoule be
1 4 3
ur o/p
1 3 4

i have seen this vedio but didn’t understood can u plzz correct my code

do u need a iterative code
??
a recursive code is simple
can help easily/faster with that one

yes iterative code using queue

okay
will take a while

i`ll share the same

void printRightView(Node* root) 
{ 
    if (!root) 
        return; 
  
    queue<Node*> q; 
    q.push(root); 
  
    while (!q.empty()) 
    {     
        // number of nodes at current level 
        int n = q.size(); 
          
        // Traverse all nodes of current level  
        for(int i = 1; i <= n; i++) 
        { 
            Node* temp = q.front(); 
            q.pop(); 
                  
            // Print the right most element  
            // at the level 
            if (i == n) 
                cout<<temp->data<<" "; 
              
            // Add left node to queue 
            if (temp->left != NULL) 
                q.push(temp->left); 
  
            // Add right node to queue 
            if (temp->right != NULL) 
                q.push(temp->right); 
        } 
    } 
}