Advertisement
Stoycho_KK

Untitled

Jan 8th, 2022
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. struct Tree {
  5. int data;
  6. Tree* left;
  7. Tree* rigth;
  8.  
  9. Tree(const int& d, Tree* l = nullptr, Tree* r = nullptr) : data(d), left(l), rigth(r) {}
  10. };
  11.  
  12. void freeTree(Tree* r) {
  13. if (r == nullptr)
  14. return;
  15. Tree* toDestroy = r;
  16. freeTree(r->left);
  17. freeTree(r->rigth);
  18. delete toDestroy;
  19. }
  20.  
  21. int heigth(const Tree* root) {
  22. if (root == nullptr)
  23. return -1;
  24. return 1 + std::max(heigth(root->left), heigth(root->rigth));
  25. }
  26.  
  27. int countNodesInSubtree(const Tree* root, int h) {
  28. if (root == nullptr) {
  29. return 0;
  30. }
  31. else if (h == 0) {
  32. return 1;
  33. }
  34.  
  35. return 1 + countNodesInSubtree(root->left, h - 1) + countNodesInSubtree(root->rigth, h - 1);
  36. }
  37.  
  38. bool isPerfectlyBalancedSubtree(const Tree* root, int h, int min = INT_MIN, int max = INT_MAX) {
  39. bool brokenBSTProperty = (root != nullptr && (root->data < min || root->data > max));
  40.  
  41. if (root == nullptr || brokenBSTProperty)
  42. return false;
  43. else if (h == 0)
  44. return true;
  45.  
  46. int lc = countNodesInSubtree(root->left, h - 1);
  47. int rc = countNodesInSubtree(root->rigth, h - 1);
  48.  
  49. return std::abs(lc - rc) < 2 &&
  50. isPerfectlyBalancedSubtree(root->left, h - 1, min, root->data) &&
  51. isPerfectlyBalancedSubtree(root->rigth, h - 1, root->data, max);
  52. }
  53.  
  54. int perfectlyBalancedCount(const Tree* root, int heigth, int globalHeigth) {
  55. if (globalHeigth < heigth || root == nullptr)
  56. return 0;
  57.  
  58. return isPerfectlyBalancedSubtree(root, heigth) +
  59. perfectlyBalancedCount(root->left, heigth, globalHeigth - 1) +
  60. perfectlyBalancedCount(root->rigth, heigth, globalHeigth - 1);
  61. }
  62.  
  63. int solution(const Tree* root, int h) {
  64. int globalHeigth = heigth(root);
  65. return perfectlyBalancedCount(root, h, globalHeigth);
  66. }
  67.  
  68. Tree* makeTree(const std::vector<int> v) {
  69. if (v.empty())
  70. return nullptr;
  71. int mid = v.size() / 2;
  72. return new Tree(v[mid],
  73. makeTree(std::vector<int>(v.begin(), v.begin() + mid)),
  74. makeTree(std::vector<int>(v.begin() + mid + 1, v.end())));
  75. }
  76.  
  77. void printBT(const std::string& prefix, const Tree* node, bool isLeft) {
  78. if (node != nullptr) {
  79. std::cout << prefix;
  80.  
  81. std::cout << (isLeft ? "L---" : "R---");
  82.  
  83. std::cout << node->data << std::endl;
  84.  
  85. printBT(prefix + (isLeft ? "L " : " "), node->left, true);
  86. printBT(prefix + (isLeft ? "R " : " "), node->rigth, false);
  87. }
  88. }
  89.  
  90. void printBT(const Tree* node) {
  91. printBT("", node, false);
  92. }
  93.  
  94. int main() {
  95. Tree* t = makeTree({ 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10, 11, 12, 13, 14, 15});
  96. printBT(t);
  97. std::cout << solution(t, 4);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement