#P2171. Hz 吐泡泡
Hz 吐泡泡
Description
One day, on a whim, Hz blew different bubbles to play with (guaranteed to be no duplicates). Because he still has homework to do, he asks you to help sort these bubbles into a tree and output its postorder traversal.
Specifically, Hz uses the binary search tree (BST) algorithm to insert the bubbles into the tree in the given order. Each insertion adds the new bubble as a leaf, and the in-order traversal of the whole tree remains in ascending order.
The binary tree after all insertions is the required tree.
Input Format
A total of lines.
- The first line contains integer .
- The second line contains integers, where the -th integer is the size of the -th bubble.
Output Format
A total of lines.
- The first line outputs the depth of the tree.
- Then output lines, one integer per line, giving the postorder traversal of the tree.
8
1 4 3 9 10 35 2 7
deep=5
2
3
7
35
10
9
4
1
Hint
- This problem has two subtasks. The first subtask has test points, each worth points. The second subtask has test points, each worth points.
- Constraints: , , and all are distinct.
Translated by ChatGPT 5
京公网安备 11011102002149号