7-9 sequence traversal of complete binary tree (25 points)

Click to view the title

7-9 sequence traversal of complete binary tree (25 points)
A binary tree is a perfect binary tree if the number of nodes in each layer reaches the maximum. For a binary tree with depth D and N nodes, if its nodes correspond to the first n nodes of the sequence traversal of the perfect binary tree with the same depth, such a tree is a complete binary tree.

Given the post order traversal of a complete binary tree, please give the sequence traversal result of this tree.

Input format:
Enter the positive integer n (≤ 30) given in the first line, that is, the number of nodes in the tree. The second line gives the post order traversal sequence, which is n positive integers no more than 100. All numbers in the same line are separated by spaces.

Output format:
Output the sequence traversal sequence of the tree in one line. All numbers shall be separated by 1 space, and there shall be no extra space at the beginning and end of the line.

Input sample:
8 91 71 2 34 10 15 55 18
No blank lines at the end
Output example:
18 34 55 71 2 10 15 91
No blank lines at the end


…… When learning data structure, say a very simple topic
I didn’t do it again.

Simulate post order traversal, and then store the data into the array


using namespace std;

int n;
int bitree[35];
void buildtree(int k)
  if(k>n) return;

int main()
  buildtree(1);// Post order tree building
  For (int i = 1; I < = n; I + +) // hierarchical output
  if(i==n) cout<