forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.1.cpp
79 lines (76 loc) · 1.74 KB
/
4.1.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
78
79
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 100;
struct Node{
int key;
Node *lchild, *rchild, *parent;
};
Node *head, *p, node[maxn];
int cnt;
int getHeight(Node *head){
if(head == NULL) return 0;
else return max(getHeight(head->lchild), getHeight(head->rchild)) + 1;
}
bool isBanlance(Node *head){
if(head == NULL) return true;
int dif = getHeight(head->lchild) - getHeight(head->rchild);
if(abs(dif) > 1)
return false;
else
return isBanlance(head->lchild) && isBanlance(head->rchild);
}
void init(){
head = p = NULL;
memset(node, '\0', sizeof(node));
cnt = 0;
}
void insert(Node* &head, int x){
if(head == NULL){
node[cnt].key = x;
node[cnt].parent = p;
head = &node[cnt++];
return;
}
p = head;
if(x < head->key)
insert(head->lchild, x);
else
insert(head->rchild, x);
}
int d = 0, num = 0, dep[maxn];
void getDepth(Node *head){
if(head == NULL) return;
++d;
getDepth(head->lchild);
if(head->lchild == NULL && head->rchild == NULL)
dep[num++] = d;
getDepth(head->rchild);
--d;
}
bool isBalance(Node *head){
if(head == NULL) return true;
getDepth(head);
int max = dep[0], min = dep[0];
for(int i=0; i<num; ++i){
if(dep[i]>max) max = dep[i];
if(dep[i]<min) min = dep[i];
}
if(max-min > 1) return false;
else return true;
}
int main(){
init();
int a[] = {
5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12
};
for(int i=0; i<12; ++i)
insert(head, a[i]);
getDepth(head);
for(int i=0; i<num; ++i)
cout<<dep[i]<<" ";
cout<<endl;
cout<<isBanlance(head)<<endl;
return 0;
}