-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path110_balanced_binary_tree.c
55 lines (43 loc) · 1.12 KB
/
110_balanced_binary_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#define max(A,B) ((A)>(B)?(A):(B))
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int height(struct TreeNode *root) {
if(root == NULL)
return 0;
int left_height = height(root->left);
if(left_height == -1)
return -1;
int right_height = height(root->right);
if(right_height == -1)
return -1;
if(abs(left_height-right_height) > 1)
return -1;
return max(left_height, right_height)+1;
}
int isBalanced(struct TreeNode *root) {
if(root == NULL)
return 1;
return height(root) >= 0;
}
int main() {
struct TreeNode *root = malloc(sizeof(struct TreeNode));
root->val = 5;
struct TreeNode *left1_1 = malloc(sizeof(struct TreeNode));
left1_1->val = 3;
struct TreeNode *left2_1 = malloc(sizeof(struct TreeNode));
left2_1->val = 1;
root->left = left1_1;
root->right = NULL;
left1_1->left = left2_1;
left1_1->right = NULL;
left2_1->left = NULL;
left2_1->right = NULL;
assert(isBalanced(root) == 0);
return 0;
}