Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "tree.h"
  3. #include <stdio.h>
  4.  
  5. int func(Tree t) {
  6.     if (t == NULL) {
  7.         return 0;
  8.     } else if (t->left == NULL && t->right == NULL) {
  9.         return 1;
  10.     }
  11.     int left = func(t->left);
  12.     int right = func(t->right);
  13.     if (abs(left - right) > 1 || left == -1 || right == -1) {
  14.         return -1;
  15.     } else {
  16.         return 1 + left + right;
  17.     }
  18. }
  19.  
  20. int TreeIsPerfectlyBalanced(Tree t) {
  21.     if (t == NULL || func(t) != -1) {
  22.         return 1;
  23.     } else {
  24.         return 0;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement