a53

kNivel1

a53
Dec 30th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #define INFINIT 2000000000
  4. using namespace std;
  5.  
  6. ifstream fin("knivel1.in");
  7. ofstream fout("knivel1.out");
  8.  
  9. struct nod{
  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. int Sum(nod * p, int i , int k)
  29. {
  30. // i reprezinta nivelul curent din arbore
  31. if(p)
  32. {
  33. if(i == k)
  34. return p->info;
  35. else
  36. return Sum(p->st , i + 1 , k) + Sum(p->dr , i + 1 , k);
  37. }
  38. else
  39. return 0;
  40. }
  41.  
  42. void stergeTot(nod * & p)
  43. {
  44. if(p != NULL)
  45. {
  46. stergeTot(p -> st);
  47. stergeTot(p -> dr);
  48. delete p;
  49. p = NULL;
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. nod * p = NULL;
  56. int k;
  57. citire(p);
  58. fin >> k;
  59. fout << Sum(p, 0 , k);
  60. stergeTot(p);
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment