#P2171. BST Construction and Postorder Traversal

    ID: 15452 Type: Default 1000ms 256MiB

BST Construction and Postorder Traversal

BST Construction and Postorder Traversal

You are given n distinct integers representing bubble values. Construct a binary search tree (BST) following the rule that the left subtree satisfies \(\text{value} \leq \text{root}\) and the right subtree satisfies \(\text{root} < \text{value}\). Since the values are all distinct, the left subtree effectively contains values smaller than the root. After constructing the BST, output its postorder traversal.

Note: The postorder traversal visits the left subtree, then the right subtree, and finally the root node.

inputFormat

The first line contains an integer \(n\) (\(1 \leq n \leq 10^5\)), the number of bubbles.
The second line contains \(n\) distinct integers separated by spaces, representing the bubble values.

outputFormat

Output a single line containing the postorder traversal of the constructed BST. The values should be separated by a single space.

sample

3
2 1 3
1 3 2