Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #include "tree.h"
  4.  
  5. int TreeHeight(Tree t) {
  6.  
  7. if (t == NULL) return 0;
  8.  
  9. int l = TreeHeight(t->left);
  10. int r = TreeHeight(t->right);
  11.  
  12. return 1 + l + r;
  13.  
  14. }
  15.  
  16. bool TreeIsPerfectlyBalanced(Tree t) {
  17.  
  18. if (t == NULL) return true;
  19.  
  20. int l = TreeHeight(t->left);
  21. int r = TreeHeight(t->right);
  22.  
  23. if (l - r < -1 || l - r > 1)
  24. return false;
  25. else
  26. return TreeIsPerfectlyBalanced(t->left)
  27. && TreeIsPerfectlyBalanced(t->right);
  28.  
  29. return true;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement