Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. int tree :: f(tree_node *root, int k)
  2. {
  3.     if(!root)
  4.         return 0;
  5.     return count_on_lvl_less_k(root,k)+f(root->left,k)+f(root->right,k);
  6. }
  7. void tree :: f4(tree_node *root, int *a, int level)
  8. {
  9.     if(!root)
  10.         return;
  11.     a[level]++;
  12.     f4(root->left, a,level+1);
  13.     f4(root->right, a,level+1);
  14. }
  15. int tree :: count_on_lvl_less_k(tree_node *root, int k)
  16. {
  17.     int *a;
  18.     int lvl=0, count_lvl = depth(root);
  19.     if(!root)
  20.         return 0;
  21.     a = new int [count_lvl+1];
  22.     for(lvl=0; lvl<count_lvl; lvl++)
  23.         a[lvl] = 0;
  24.     f4(root,a,0);
  25.     for(lvl=0; lvl<count_lvl; lvl++)
  26.     {
  27.         if(a[lvl] > k)
  28.         {
  29.             delete a;
  30.             return 0;
  31.         }
  32.     }
  33.     delete a;
  34.     return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement