Iterative Pre, Post And Inorder Traversals Of Binary Tree

void iterativePrePostInTraversal(Node *root)
{
    // pair of node and number of visits to the i-th node.
    //  if number of visits=0, then print in pre and move to left.
    //  if number of visits=1, then print in In and move to right.
    //  if number of visits=2, then print in post and pop the value at top;

    stack<pair<Node *, int>> st;
    vector<int> pre, post, in;

    st.push({root, 0});

    pair<Node *, int> temp;

    while (!st.empty())
    {
        temp = st.top();

        if (temp.second == 0)
        {
            st.top().second++;
            pre.push_back(temp.first->data);
            if (temp.first->left)
                st.push({temp.first->left, 0});
        }


        else if (temp.second == 1)
        {
            st.top().second++;
            in.push_back(temp.first->data);
            if (temp.first->right)
                st.push({temp.first->right, 0});
        }


        else
        {
            st.pop();
            post.push_back(temp.first->data);
        }
    }

   
    for (auto it : pre)
        cout << it << " ";
    cout << endl;
    for (auto it : in)
        cout << it << " ";
    cout << endl;
    for (auto it : post)
        cout << it << " ";
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree