Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream in("date.in");
  6.  
  7. struct node {
  8. int inf;
  9. node *st, *dr;
  10. };
  11. node *prim = new node;
  12. bool ok;
  13. node *creare() {
  14. int x;
  15. in >> x;
  16.  
  17. if ( x == 0 )
  18. return NULL;
  19. else {
  20. node *nou;
  21. nou = new node;
  22. nou->inf = x;
  23. if(!ok)
  24. prim = nou;
  25. ok = 1;
  26. nou->st = creare();
  27. nou->dr = creare();
  28. return nou;
  29. }
  30. }
  31.  
  32. int maxim ( node *p ) {
  33. if ( p != NULL ) {
  34. int m = max ( maxim ( p->st ), maxim ( p->dr ) );
  35.  
  36. if ( m == 0 )
  37. m = p->inf;
  38.  
  39. return m;
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. int suma ( node *p ) {
  46. if ( p != NULL ) {
  47. int s1, s2;
  48. s1 = suma ( p->st );
  49. s2 = suma ( p->dr );
  50. return s1 + s2 + p->inf;
  51. } else
  52. return 0;
  53. }
  54. void nivel(node*p,int k,int niv){
  55. if(p!=NULL){
  56. if(k==niv)
  57. {
  58.  
  59. cout<<p->inf<<" ";
  60. return;
  61. }
  62. nivel(p->st,k+1,niv);
  63. nivel(p->dr,k+1,niv);
  64. }
  65. }
  66. int main() {
  67. creare();
  68. cout<<maxim(prim)<<'\n';
  69. cout<<suma(prim)<<'\n';
  70. int niv;
  71. cin>>niv;
  72. nivel(prim,1,niv);
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement