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 *insert(node *root, int data) {
- if (!root) {
- node *newNode = new node;
- newNode->data = data;
- newNode->left = newNode->right = NULL;
- return newNode;
- }
- if (data < root->data)
- root->left = insert(root->left, data);
- else
- root->right = insert(root->right, data);
- 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);
- node *root = NULL;
- for (int i = 0; i < n; ++i) {
- int x;
- scanf("%d", &x);
- root = insert(root, x);
- }
- postorder(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment