fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node*left;
  6. Node*right;
  7. Node(int x) {
  8. data=x;
  9. left=right=nullptr;
  10. }
  11. };
  12. Node* insert(Node* root, int x) {
  13. if(root==nullptr) return new Node(x);
  14. if(x<root->data) root->left=insert(root->left, x);
  15. else root->right=insert(root->right, x);
  16. return root;
  17. }
  18. void postor(Node* root) {
  19. if(root==nullptr) return;
  20. postor(root->left);
  21. postor(root->right);
  22. cout << root->data << " ";
  23. }
  24. int main() {
  25. int T;
  26. cin >> T;
  27. while(T--) {
  28. int n;
  29. cin >> n;
  30. Node* root=nullptr;
  31. int a[n];
  32. for(int i=0; i<n; i++) {
  33. int x;
  34. cin >> x;
  35. root=insert(root, x);
  36. }
  37. postor(root);
  38. cout << endl;
  39. }
  40. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty