-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_balanced.cpp
More file actions
73 lines (64 loc) · 1.09 KB
/
check_balanced.cpp
File metadata and controls
73 lines (64 loc) · 1.09 KB
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
#include <iostream>
using namespace std;
template <class T>
class Node{
public:
Node();
~Node();
T data;
Node<T>* left;
Node<T>* right;
};
template <class T>
Node<T>::Node(){
left = NULL;
right = NULL;
}
template <class T>
Node<T>::~Node(){
delete data;
}
int checkBalanced(Node<int>* node){
if(!node){
return 0;
}
int left = checkBalanced(node->left);
if(left == -1){
return -1;
}
int right = checkBalanced(node->right);
if(right == -1){
return -1;
}
if(abs(left - right) > 1){
return -1;
}
return max(left, right) + 1;
}
int main(){
Node<int>* root = new Node<int>();
root->data = 2;
Node<int>* a = new Node<int>();
a->data = 3;
Node<int>* b = new Node<int>();
b->data = 4;
Node<int>* c = new Node<int>();
c->data = 5;
Node<int>* d = new Node<int>();
d->data = 6;
Node<int>* e = new Node<int>();
e->data = 7;
Node<int>* f = new Node<int>();
f->data = 8;
Node<int>* g = new Node<int>();
g->data = 9;
root->left = a;
root->right = b;
a->left = c;
a->right = e;
a->left->left = g;
b->left = f;
b->right = d;
cout << checkBalanced(root) << endl;
return 0;
}