VasilM

alg_upr_4

Mar 12th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, *sb;
  6.  
  7. void initializeTree() {
  8.      
  9.     cin >> n;
  10.     sb = new int[n];
  11.     sb[0] = n;
  12.     for(int i=1; i<=n; i++) {
  13.         cin >> sb[i];
  14.     }
  15. }
  16.  
  17. void printRoots() {
  18.      
  19.     for(int i=1; i<=n; i++) {
  20.         if(sb[i] == 0) {
  21.             cout << i << " is root\n";
  22.         }
  23.     }
  24. }
  25.  
  26. void printLeafs() {
  27.      
  28.     for(int i=1; i<=n; i++) {
  29.         for(int j=1; j<=n; j++) {
  30.             if(i == sb[j]) {
  31.                 i++;
  32.                 continue;
  33.             }
  34.             if(j == n) {
  35.                 cout << i << " is a leaf\n";
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. int main() {
  42.    
  43.     initializeTree();
  44.     printRoots();
  45.     printLeafs();
  46.  
  47.     delete sb;
  48.     return EXIT_SUCCESS;
  49. }
  50.  
  51.  
  52. /*
  53. 8
  54. 0
  55. 1
  56. 8
  57. 2
  58. 4
  59. 2
  60. 1
  61. 6
  62. */
Advertisement
Add Comment
Please, Sign In to add comment