Advertisement
Sanlover

Untitled

Dec 2nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. void Tree::func() {
  2.   const size_t nFloors = height() + 1;
  3.   std::pair<int, int>* array = new std::pair<int, int>[nFloors];
  4.   for (size_t i = 0; i < nFloors; i++) array[i].first = array[i].second = 0;
  5.   size_t id = 0;
  6.   nNodes(head, array, id);
  7.  
  8.   for (size_t i = 0; i < nFloors; i++) {
  9.     std::cout << "Floor = " << i + 1 << " | nNodes =  " << array[i].second
  10.               << " | nLeafs = " << array[i].first << std::endl;
  11.   }
  12. }
  13. void Tree::nNodes(Node* node, std::pair<int, int>*& array, size_t& floor_id) {
  14.   if (node == nullptr) return;
  15.  
  16.   if (node->left == nullptr && node->right == nullptr)
  17.     array[floor_id].first++;
  18.   else {
  19.     array[floor_id].second++;
  20.     floor_id++;
  21.  
  22.     nNodes(node->left, array, floor_id);
  23.     nNodes(node->right, array, floor_id);
  24.     floor_id--;
  25.   }
  26.   return;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement