Tree: Level Order Traversal

  • + 0 comments
    void levelOrder(Node * root) {
    
        queue<Node*>q;
        q.push(root);
        while(!q.empty()){
            int size = q.size(); 
            for(int i=0;i<size;i++){
    
              Node* popnode=q.front();
              q.pop();
              cout<<popnode->data<<" ";
              if(popnode->left!=NULL){
                  q.push(popnode->left);
              }
              if(popnode->right!=NULL){
                 q.push(popnode->right);
              }
            }
    
        }
    
    
    }