Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1010;
- int pre[N];
- struct node {
- node *left;
- node *right;
- int data;
- };
- node *solve(int l, int r) {
- if (l > r)
- return NULL;
- node *root = new node;
- root->data = pre[l];
- for (int i = l+1; i <= r+1; ++i) {
- if (pre[i] > pre[l] || i == r+1) {
- root->left = solve(l+1, i-1);
- root->right = solve(i, r);
- break;
- }
- }
- return root;
- }
- void postorder(node *root) {
- if (root == NULL)
- return;
- postorder(root->left);
- postorder(root->right);
- printf("%d ", root->data);
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- for (int i = 0; i < n; ++i)
- scanf("%d", &pre[i]);
- node *root = solve(0, n-1);
- postorder(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement