AquaBlitz11

Mr. Duck's BST - Easy BST

Mar 18th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1010;
  5. int pre[N];
  6.  
  7. struct node {
  8.     node *left;
  9.     node *right;
  10.     int data;
  11. };
  12.  
  13. node *insert(node *root, int data) {
  14.  
  15.     if (!root) {
  16.         node *newNode = new node;
  17.         newNode->data = data;
  18.         newNode->left = newNode->right = NULL;
  19.         return newNode;
  20.     }
  21.     if (data < root->data)
  22.         root->left = insert(root->left, data);
  23.     else
  24.         root->right = insert(root->right, data);
  25.     return root;
  26. }
  27.  
  28. void postorder(node *root) {
  29.     if (root == NULL)
  30.         return;
  31.     postorder(root->left);
  32.     postorder(root->right);
  33.     printf("%d ", root->data);
  34. }
  35.  
  36. int main()
  37. {
  38.     int n;
  39.     scanf("%d", &n);
  40.     node *root = NULL;
  41.     for (int i = 0; i < n; ++i) {
  42.         int x;
  43.         scanf("%d", &x);
  44.         root = insert(root, x);
  45.     }
  46.  
  47.     postorder(root);
  48.  
  49.     return 0;
  50. }
Add Comment
Please, Sign In to add comment