Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct Tree {
- int info;
- Tree *parent, *left, *right;
- };
- Tree *tree;
- void Insert(Tree **l, int x, Tree *parent) {
- Tree *p;
- if(*l == NULL) {
- p = new Tree;
- p -> info = x;
- p -> left = p -> right = nullptr;
- p -> parent = parent;
- *l = p;
- return;
- }
- if(x < (*l) -> info)
- Insert(&((*l) -> left), x, *l);
- else
- Insert(&((*l) -> right), x, *l);
- }
- void PostOrder_traversal(Tree *l) {
- if (l != nullptr) {
- PostOrder_traversal(l -> right);
- PostOrder_traversal(l -> left);
- cout << l -> info << '\n';
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int N;
- cin >> N;
- vector < int > a(N);
- for(int& x : a)
- cin >> x;
- for(int i = N - 1; i >= 0; --i)
- Insert(&tree, a[i], nullptr);
- PostOrder_traversal(tree);
- }
Add Comment
Please, Sign In to add comment