c++binary-treeinorderpreorderpostorder

How to Print the Postorder Traversal of a Binary Tree Given the Preorder and Inorder Traversals?


I was given a task of trying to print the postorder traversal of a binary tree when you are given the preorder and the inorder traversals.

I went online to search up the problem, but the only results that I found was this GeekForGeeks article. However, I found this article quite confusing and I didn't understand how to solve this problem even after reading the article. Can someone help simplify the article into simpler words? Thanks!


Solution

  • This is impossible in the general case. Consider:

      A
    A   B
    

    Let's label these as

      1
    2   3
    

    Now consider:

    A (1)
      A (2)
        B (3)
    

    Given preorder and inorder trees as AAB will not have an unambigous postorder representation.