Advertisement
a53

BiFrunze1

a53
Dec 28th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. ifstream fin("bifrunze1.in");
  6. ofstream fout("bifrunze1.out");
  7. int nt[5001],n=-1;
  8. struct nod
  9. {
  10. int info;
  11. nod * st, *dr;
  12. };
  13.  
  14. void citire(nod * & p)
  15. {
  16. int x;
  17. fin>>x;
  18. if(x==0)
  19. p=NULL;
  20. else
  21. {
  22. p=new nod;
  23. p->info=x;
  24. citire(p->st);
  25. citire(p->dr);
  26. }
  27. }
  28.  
  29. void preordine(nod * p)
  30. {
  31. if(p!=NULL)
  32. {
  33. if(p->st==NULL&&p->dr==NULL)
  34. n++,nt[n]=p->info;
  35. preordine(p->st);
  36. preordine(p->dr);
  37. }
  38. }
  39.  
  40. void stergeTot(nod * & p)
  41. {
  42. if(p!=NULL)
  43. {
  44. stergeTot(p->st);
  45. stergeTot(p->dr);
  46. delete p;
  47. p=NULL;
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. nod * p = NULL;
  54. citire(p);
  55. preordine(p);
  56. n++;
  57. sort(nt,nt+n);
  58. for(int i=0;i<n;i++)
  59. fout<<nt[i]<<' ';
  60. fout<<endl;
  61. stergeTot(p);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement