Tuesday, November 1, 2011

Infibeam Question: Reverse Level Order Traversal of Binary Tree

void BSTree::reverseLevelOrderTraversal(Node* node)
{
    queue<Node*> qu;
    stack<int> st;
    qu.push(node);
    while(!qu.empty())
    {
        Node* temp = qu.front();
        st.push(temp->data);
        if(temp->right)
            qu.push(temp->right);
        if(temp->left)
            qu.push(temp->left);
        qu.pop();
    }
    while(!st.empty())
    {
        cout<<st.top()<<'\t';
        st.pop();
    }
}

No comments:

Post a Comment