BST Inorder to Preorder and Postorder

41 views Asked by At

I have an inorder form of binary search tree. I need to understand how the tree will be looking like. Also I need the preorder and post order of the same tree. Need help on this. In Order : 7, 5, 3, 2, 4, 6, 10, 9, 8, 12, 11, 15

How the tree will be looking like? What will be the preorder and postorder for this tree?

Not able to solve this

1

There are 1 answers

0
cuong02n On

Impossible for get Post-order and Pre-order with only In-order in BST.

Ex: If we have 2 BSTs:

      6                 5
     / \               / \
    2   8             2   6
   / \   \           /   / \
  1   5   10        1   8   10

In-order of them are 1, 2, 5, 6, 8, 10.

Pre-order of the left tree is 6, 2, 1, 5, 8, 10.

Pre-order of the right tree is 5, 2, 1, 6, 8, 10.

That meaning, with 1 In-order listed, you can build many trees.

There are 3 traversals, you can find 1 if you know 2 others, or know something else, ex It is the Balance tree, or Red-Black tree.

Note: The list you provided: [7, 5, 3, 2, 4, 6, 10, 9, 8, 12, 11, 15] cannot be In-order of a Binary Search Tree.