kburnik

C++ - Binarno stablo - suma u preorder obilasku

Oct 6th, 2012
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. TEMA : Binarno stablo i suma svih cvorova u preorder obilasku
  4.  
  5. Autor zadatka: Kristijan Burnik
  6.  
  7. Autor rjesenja: Kristijan Burnik
  8.  
  9. Datum rjesavanja: 2012-10-06
  10.      
  11. Primjer:
  12. 5            
  13. 10 20 30 40 50            
  14. 2
  15. 1 2 3        
  16. 2 4 5      
  17.  
  18. */
  19. #include <iostream>
  20. #include <cstdlib>
  21. #include <vector>
  22.  
  23. using namespace std;
  24.  
  25. typedef struct node {
  26.         int val;
  27.         node *left;
  28.         node *right;        
  29. } node;
  30.  
  31. /////////////////////////////////
  32.  
  33. int suma(node *p) {
  34.     if (p == NULL) return 0;
  35.     return p->val + suma(p->left) + suma(p->right);  
  36. }
  37.  
  38.  
  39. int main() {
  40.  
  41.     int n;
  42.     cin >> n;
  43.    
  44.     vector < node > stablo;    
  45.     stablo.resize( n + 1 ) ;
  46.     for (int i = 1;  i <= n; i++) {
  47.         cin >> stablo[i].val;
  48.     }
  49.    
  50.     int m;
  51.     cin >> m;
  52.     int p,l,r;
  53.     for (int i = 0 ; i < m ; i++) {
  54.         cin >> p >> l >> r;
  55.         stablo[p].left = &stablo[l];
  56.         stablo[p].right  = &stablo[r];
  57.     }
  58.    
  59.     cout << "suma: " << suma(&stablo[1]);
  60.     cout << endl;
  61.  
  62.     system("pause");
  63.     return 0;
  64. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×