fork(1) download
  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<string.h>
  6. #include<math.h>
  7. #include<vector>
  8. #include<stack>
  9. #include<queue>
  10. #include<map>
  11. #include<stdio.h>
  12. #include<set>
  13. #include<cstring>
  14. #include<cstdlib>
  15. using namespace std;
  16. struct node{
  17. int va;
  18. node* left;
  19. node* right;
  20. node():left(nullptr),right(nullptr),va(NULL){}
  21. };
  22. node *solve(node *head,int a){
  23. node *ptr=new node;
  24. ptr->va=a;
  25. if(head->va==0){
  26. return ptr;
  27. }
  28. while(head->va!=0){
  29. if(head->va>a){
  30. head=head->left;
  31. }
  32. else if(head->va<a){
  33. head=head->right;
  34. }
  35. }
  36. head=ptr;
  37. return ptr;
  38. }
  39. void print(node *root){
  40. if(root->va==0){
  41. return;
  42. }
  43. print(root->left);
  44. print(root->right);
  45. cout<<root->va<<endl;
  46. }
  47. int main(){
  48. node *root,*head;
  49.  
  50. int a;
  51. int i=0;
  52. while(cin>>a){
  53. head=solve(root,a);
  54. if(i==0){
  55. root=head;
  56. }
  57. i++;
  58. }
  59. print(root);
  60. return 0;
  61. }
Runtime error #stdin #stdout 0s 3460KB
stdin
50
30
24
5
28
45
98
52
60
stdout
Standard output is empty