Reverse Level Order Traversal | Practice | GeeksforGeeks

Given a binary tree of size N, find its reverse level order traversal. ie- the traversal must begin from the last level. 
Example 1:
Input :
        1
      /   \
     3     2

Output: 3 2 1
Explanation:
Traversing level 1 : 3 2
Traversing level 0 : 1
Example 2:
Input :
       10
      /  \
     20   30
    / \ 
   40  60

Output: 40 60 20 30 10
Explanation:
Traversing level 2 : 40 60
Traversing level 1 : 20 30
Traversing level 0 : 10

Your Task:  
You dont need to read input or print anything. Complete the function reverseLevelOrder() which takes the root of the tree as input parameter and returns a list containing the reverse level order traversal of the given tree.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 10^4
0
sibajit1176be20
This comment was deleted.
0
namangoel8016 days ago
EASIEST SOLUTION
SMALL CHANGE IN NORMAL LEVEL ORDER TRAVERSAL

class Tree
{
    public ArrayList<Integer> reverseLevelOrder(Node node) 
    {
        Queue<Node> q = new LinkedList<>();
        ArrayList<Integer> al = new ArrayList<>();
        q.add(node);
        while(q.size()!=0){
            Node rem = q.remove();
            al.add(0,rem.data);
            if(rem.right!=null) q.add(rem.right);
            if(rem.left!=null) q.add(rem.left);
        }
        return al;
    }
}      
0
aishapervin032 weeks ago
vector<int> reverseLevelOrder(Node *root)
{
      vector<int>ans;
      stack<vector<int>>st;
      queue<Node*>q;
      q.push(root);
      vector<int>t;
      while(q.size()>0)
      {
          int n=q.size();
          
          while(n--)
          {   
              
              Node* temp=q.front();
              q.pop();
              t.push_back(temp->data);
              
              if(temp->left)
               q.push(temp->left);
               
               if(temp->right)
               q.push(temp->right);
             
          }
          st.push(t);
          t.clear();
      }
      while(st.size()>0)
      {
          for(int i=0;i<st.top().size();i++)
              ans.push_back(st.top()[i]);
        st.pop();
          
      }
      return ans;
}
0
rawatchirag7122 weeks ago
vector<int> reverseLevelOrder(Node *root){          vector<int>v;     queue<Node *>q;     q.push(root);     while(!q.empty()){          Node *temp=q.front();
          q.pop();           if(temp->right)          {              q.push(temp->right);          }          if(temp->left)          {              q.push(temp->left);          }                               v.push_back(temp->data);     }     reverse(v.begin(),v.end());    return v;}
-1
mnsjhansi3 weeks ago
def reverseLevelOrder(root):
    # code here
    queue = []
    ans = []
    queue.append(root)
    
    while len(queue) > 0:
        node = queue.pop(0)
        ans.append(node.data)
        if node.right is not None:
            queue.append(node.right)
        if node.left is not None:
            queue.append(node.left)
    
    return ans[::-1]
0
jainmuskan5653 weeks ago
vector<int> reverseLevelOrder(Node *root){   queue<Node *> q;   stack<Node *> s;   q.push(root);   while(!q.empty()){       Node * temp= q.front();       q.pop();       s.push(temp);       if(temp->right){           q.push(temp->right);       }       if(temp->left){           q.push(temp->left);       }          }   vector<int>v;   while(!s.empty()){       Node *temp= s.top();       s.pop();       v.push_back(temp->data);   }   return v;
+2
sagrikasoni3 weeks ago
class Tree
{
    public ArrayList<Integer> reverseLevelOrder(Node node) 
    {
     ArrayList<Integer> arr = new ArrayList<Integer>();
       if(node==null) return arr;
       Queue<Node> q = new LinkedList<Node>();
       q.add(node);
       while(!q.isEmpty()){
           Node curr = q.poll();
           arr.add(curr.data);
           if(curr.right!=null)
           q.add(curr.right);
           if(curr.left!=null)
           q.add(curr.left);
       }
       Collections.reverse(arr);
       return arr;
0
betulaltindis1 month ago
//Java Solution
 
class Tree{   public ArrayList<Integer> reverseLevelOrder(Node node)    {       ArrayList<Integer> list = new ArrayList<>();       Stack<Node> s= new Stack<Node>();       Queue<Node> q= new LinkedList<Node>();       q.add(node);              while(q.size()!=0){           Node tempNode = q.poll();           s.push(tempNode);                      if(tempNode.right!= null)               q.add(tempNode.right);           if(tempNode.left!= null)               q.add(tempNode.left);       }              while(s.size()!=0)           list.add(s.pop().data);                  return list;   }}      
0
balkishanjaipal1 month ago
 
C++ Solution
vector<int> reverseLevelOrder(Node *root){   // code here   //Your code here     vector<int> v;          queue<Node*> q;     q.push(root);               while(!q.empty())     {         Node *f =q.front();         q.pop();          v.push_back(f->data);                   if(f->right)         {             q.push(f->right);         }         if(f->left)         {             q.push(f->left);                      }                     }     reverse(v.begin() ,v.end());     return v;}
0
shkoli1 month ago
void rotUtil(Node* root, vector<int> &vec)
{
    if (root->left == NULL || root->right == NULL){
        return;
    }
    rotUtil(root->left, vec);
    rotUtil(root->right, vec);
    vec.push_back(root->left->data);
    vec.push_back(root->right->data);
   
}
 
vector<int> reverseLevelOrder(Node *root)
{
    // code here
    vector<int> vec;
   rotUtil(root, vec);
   vec.push_back(root->data);
    return vec;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.