Alex_tz307

1136. Parliament - Timus

Nov 4th, 2020 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Tree {
  6.     int info;
  7.     Tree *parent, *left, *right;
  8. };
  9.  
  10. Tree *tree;
  11.  
  12. void Insert(Tree **l, int x, Tree *parent) {
  13.     Tree *p;
  14.     if(*l == NULL) {
  15.         p = new Tree;
  16.         p -> info = x;
  17.         p -> left = p -> right = nullptr;
  18.         p -> parent = parent;
  19.         *l = p;
  20.         return;
  21.     }
  22.     if(x < (*l) -> info)
  23.         Insert(&((*l) -> left), x, *l);
  24.     else
  25.         Insert(&((*l) -> right), x, *l);
  26. }
  27.  
  28. void PostOrder_traversal(Tree *l) {
  29.     if (l != nullptr) {
  30.         PostOrder_traversal(l -> right);
  31.         PostOrder_traversal(l -> left);
  32.         cout << l -> info << '\n';
  33.     }
  34. }
  35.  
  36. int main() {
  37.     ios_base::sync_with_stdio(false);
  38.     cin.tie(nullptr);
  39.     cout.tie(nullptr);
  40.     int N;
  41.     cin >> N;
  42.     vector < int > a(N);
  43.     for(int& x : a)
  44.         cin >> x;
  45.     for(int i = N - 1; i >= 0; --i)
  46.         Insert(&tree, a[i], nullptr);
  47.     PostOrder_traversal(tree);
  48. }
  49.  
Add Comment
Please, Sign In to add comment