Tree: Level Order Traversal

  • + 0 comments
    void topView(Node* root) {
        std::queue<std::pair<int, Node*>> q;
        q.push(std::make_pair(0,root));
    
        while (!q.empty())
        {
            auto i = q.front();
            if(i.second != nullptr)
            {
                q.push(std::make_pair(i.first-1, i.second -> left));
                q.push(std::make_pair(i.first+1, i.second -> right));
                std::cout<<i.second->data<<' ';    
            }
            q.pop();
            i=q.front();
    
        }
    
        
    }