Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define el endl
- #define umi unordered_map<int, int>
- #define umll unordered_map<ll, ll>
- #define all(vect) vect.begin(), vect.end()
- #define reset(A) memset(A, 0, sizeof(A))
- const int mod = 1e9 + 7;
- using namespace std;
- struct node
- {
- int val;
- node *l, *r;
- };
- node *create(int x)
- {
- node *p = new node;
- p->val = x;
- p->l = NULL;
- p->r = NULL;
- return p;
- }
- node *addL(node *p, int x)
- {
- node *temp = p;
- node *te1 = create(x);
- temp->l = te1;
- return temp;
- }
- node *addR(node *p, int x)
- {
- node *temp = p;
- node *te1 = create(x);
- temp->r = te1;
- return temp;
- }
- void travel(node *root, int x)
- {
- node *p = root;
- if(p->val < x && p->l == NULL)
- p = addL(p, x);
- else if(p->val > x && p->r == NULL)
- p = addR(p, x);
- else if(p->val < x && p->l != NULL)
- travel(p->l, x);
- else if(p->val > x && p->r != NULL)
- travel(p->r, x);
- }
- void post(node *root)
- {
- node *p = root;
- if(p->r != NULL)
- post(p->r);
- if(p->l != NULL)
- post(p->l);
- cout << p->val << " ";
- }
- void solve()
- {
- int n;
- cin >> n;
- int a;
- cin >> a;
- n--;
- node *root = create(a);
- node *te = root;
- while(n--)
- {
- cin >> a;
- travel(te, a);
- }
- node *tem0 = root;
- post(tem0);
- cout << el;
- }
- int main()
- {
- int t = 1;
- cin >> t;
- while(t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement