[Solution]Level order traversal

void Levelorder(struct node* p)
{
    stack<struct node*> s;
    queue<struct node*> q;
     struct node* temp;
    s.push(p);
    do
    {
        while(!s.empty())
        {
            temp = s.top();
            cout<<temp->data<<endl;
            s.pop();
              if(temp->left)
                q.push(temp->left);
             if(temp->right)
                q.push(temp->right);


        }
        while(!q.empty())
        {
            temp = q.front();
            cout<<temp->data<<endl;
            q.pop();
           if(temp->left)
                s.push(temp->left);
            if(temp->right)
                s.push(temp->right);
        }
    }while((!s.empty()) || (!q.empty()));
}

Comments

Popular posts from this blog

Circular game survival