/
balanced-binary-tree.cpp
77 lines (64 loc) · 1.88 KB
/
balanced-binary-tree.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void traverse(TreeNode *root, int *max_depth, int depth) {
if (root == NULL)
return;
if (root->left == NULL && root->right == NULL) {
if (*max_depth <= depth)
*max_depth = depth;
}
traverse(root->left, max_depth, depth + 1);
traverse(root->right, max_depth, depth + 1);
}
bool isBalanced(TreeNode *root) {
int left_depth = 0;
int right_depth = 0;
if (root == NULL)
return true;
if (root->left == NULL && root->right == NULL)
return true;
else if (root->left == NULL && root->right != NULL) {
traverse(root->right, &right_depth, 0);
if (right_depth > 0)
return false;
else
return true;
} else if (root->left != NULL && root->right == NULL) {
traverse(root->left, &left_depth, 0);
if (left_depth > 0)
return false;
else
return true;
} else {
traverse(root->left, &left_depth, 0);
traverse(root->right, &right_depth, 0);
if (abs(left_depth - right_depth) <= 1)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
}
};
int main(int argc, char **argv)
{
Solution *sol = new Solution();
TreeNode n0(0), n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7), n8(8);
n0.left = &n1;
n0.right = &n2;
n1.left = &n3;
n1.right = &n4;
n3.left = &n5;
n3.right = &n6;
// n7.left = &n8;
printf("ret %d\n", sol->isBalanced(&n0));
}