If the pre-order of a binary search tree is [P, A, R, S], How to recognize [R, S, A, P] belongs to in-order or post-order? If is post-order how to find out is (Left, Right, Root) or (Right, Left, Root)?
Asked
Active
Viewed 97 times
4
-
Programatically? What about just construct that tree from the input data and then traverse it with `in-`/ `post-` order and get the results? Its `O(n)` – libik Jul 31 '18 at 08:20
-
Can you come up with a code? We can help you with this. `How to recognize [R, S, A, P] belongs to in-order or post-order?`- Build the tree using preorder traversal and Binary search tree property.Then do a inorder and postorder of it and which ever matches with `[R, S, A, P] `, it is that order. – nice_dev Jul 31 '18 at 09:46
2 Answers
2
In order of a binary search tree will always be in sorted order. As [R, S, A, P] is not in sorted order, it should be post order.

learner
- 1,952
- 7
- 33
- 62
2
If the pre-order traversal is [P, A, R, S], then P must be the root. And since it's a binary search tree, then A must be the left child and R and S must be in the right subtree. That gives you two possible trees:
P P
A R A S
S R
A reverse post-order traversal of the second tree will provide the sequence [R, S, A, P].

Jim Mischel
- 131,090
- 20
- 188
- 351