Level order traversal | Practice | GeeksforGeeks

Given a binary tree, find its level order traversal.
Level order traversal of a tree is breadth-first traversal for the tree.

Example 1:
Input:
    1
  /   \ 
 3     2
Output:1 3 2

Example 2:
Input:
        10
     /      \
    20       30
  /   \
 40   60
Output:10 20 30 40 60


Your Task:
You don't have to take any input. Complete the function levelOrder() that takes the root node as input parameter and returns a list of integers containing the level order traversal of the given Binary Tree.

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

Constraints:
1 ≤ Number of nodes ≤ 105
1 ≤ Data of a node ≤ 105
0
superrhitik4584 hours ago
//                                           Jai Shri Ram
class Solution{   public:   //Function to return the level order traversal of a tree   vector<int> v;   void order(Node* node)   {       queue<Node*> q;       q.push(node);       while(q.empty()==false)       {           Node* curr=q.front();           v.push_back(curr->data);//cout<<curr->data<<" ";           q.pop();           if(curr->left!=NULL)           q.push(curr->left);           if(curr->right!=NULL)           q.push(curr->right);       }   }   vector<int> levelOrder(Node* node)   {     //Your code herevector<int> levelorder(Node* root,vector<int> v)                order(node);       vector<int> vi=v;       v.clear();       return vi;      }};
0
shubham2110199715 hours ago
static ArrayList <Integer> levelOrder(Node node) 
   {
       ArrayList <Integer>arr=new ArrayList <Integer>();
      Queue<Node>q=new LinkedList<>();
      q.add(node);
      while(q.isEmpty()==false){
          Node curr=q.poll();
          arr.add(curr.data);
          if(curr.left!=null)
             q.add(curr.left);
           if(curr.right!=null)
             q.add(curr.right);
      }
      return arr;
   }
0
pc5173051 day ago
vector<int> levelOrder(Node* node)   {     //Your code here     vector<int>v;     queue<Node*>q;         // v.push_back(node->data);     q.push(node);     while(!q.empty())     {         Node *p=q.front();         q.pop();            v.push_back(p->data);            if(p->left)            {                q.push(p->left);            }            if(p->right)            {                q.push(p->right);            }                       }return v;   }};
0
ayush94602461255 days ago
Easy CPP solution:
 
class Solution{   public:   //Function to return the level order traversal of a tree.   vector<int> levelOrder(Node* node)   {     //Your code here     vector<int>ans;     queue<Node*>q;     q.push(node);     q.push(NULL);     while(!q.empty()){         Node* curNode=q.front();         q.pop();         if(curNode==NULL){             if(q.empty()){                 break;             }else{                 q.push(NULL);             }         }else{             ans.push_back(curNode->data);             if(curNode->left){                 q.push(curNode->left);             }if(curNode->right){                 q.push(curNode->right);             }         }     }return ans;   }};
0
ishitaamod1 week ago
class Solution
{
    static ArrayList <Integer> levelOrder(Node node) 
    {
        // Your code here
        Queue<Node> queue=new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<>();
        
        if(node==null)
          return list;
          
        queue.offer(node);
        while(!queue.isEmpty())
        {
            if(queue.peek().left!=null)
            {
                queue.offer(queue.peek().left);
            }
            if(queue.peek().right!=null)
            {
                queue.offer(queue.peek().right);
            }
            
           list.add(queue.poll().data);
        }
        
        
       return list;
        
    }
}
0
sibajit1176be201 week ago
 static void lebelorder() {  if(root == null)   return;   Queue<Node> queue= new LinkedList<Node>();   queue.add(root);   while(!queue.isEmpty()) {    Node temp=queue.poll();    System.out.print(temp.data+" ");    if(temp.left!=null) {          queue.add(temp.left);    }    if(temp.right!=null) {     queue.add(temp.right);    }   }   }
0
gulshan991 week ago
class Solution
{
    public:
    //Function to return the level order traversal of a tree.
    vector<int> levelOrder(Node* node)
    {
      //Your code here
      vector<int>ans;
      queue<Node*>q;
      if(node){
          q.push(node);
      }
      while(!q.empty()){
       
          for(int i = 0 ;i<q.size();i++){
              Node*temp = q.front();
              ans.push_back(temp->data);
              if(temp->left){
                  q.push(temp->left);
              }
              if(temp->right){
                  q.push(temp->right);
              }
              q.pop();
          }
         
      }
      return ans;
    }
};
0
rohanmeher1642 weeks ago
//Java Solution
 
class Solution{   //Function to return the level order traversal of a tree.   static ArrayList <Integer> levelOrder(Node node)    {    ArrayList<Integer> list=new ArrayList<Integer>();       if(node==null)           return new ArrayList<Integer>();       Queue<Node> q=new LinkedList<Node>();       q.add(node);              while(!q.isEmpty())       {           int n=q.size();           for(int i=1;i<=n;i++)           {  Node t=q.poll();              list.add(t.data);                              if(t.left!=null) q.add(t.left);                if(t.right!=null) q.add(t.right);                           }       }       return list;   }} 
0
rimpisaksham2 weeks ago
best cpp soln . runtime 0.1 sec
class Solution{   public:   //Function to return the level order traversal of a tree.         vector<int> levelOrder(Node* node)   {     //Your code here     vector<int> ans;     queue<Node*>q;     q.push(node);     q.push(NULL);     while(!q.empty())     {         Node* ptr=q.front();         q.pop();         if(ptr!=NULL){             ans.push_back(ptr->data);             if(ptr->left!=NULL){                 q.push(ptr->left);             }             if(ptr->right!=NULL){                 q.push(ptr->right);             }         }                  if(!q.empty())         {             if(q.front()==NULL){                 q.pop();                                  if(q.size()>0){                     q.push(NULL);                 }             }                     }             }                return ans;        }};
0
dipanshusharma93132 weeks ago
// java solution
class Solution{   //Function to return the level order traversal of a tree.   static ArrayList <Integer> levelOrder(Node node)    {       // Your code here       ArrayList <Integer> di = new ArrayList <>();       if(node == null){           return di;       }       Queue<Node> bh = new LinkedList<>();              bh.add(node);          while(!bh.isEmpty()){           Node currno = bh.remove();               di.add(currno.data);               if(currno.left != null){                   bh.add(currno.left);               }               if(currno.right != null){                   bh.add(currno.right);               }           }       return di;   }}
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.