-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisBT-heap.cpp
70 lines (56 loc) · 1.57 KB
/
isBT-heap.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
// Structure of node
/*struct Node {
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
left = right = NULL;
}
};*/
class Solution {
public:
int countNodes(struct Node* root){
if(root==NULL){
return 0;
}
int sum = 1 + countNodes(root->left) + countNodes(root->right);
return sum;
}
bool isCBT(struct Node* root, int idx, int totalNodes){
if(root==NULL){
return true;
}
if(idx>=totalNodes){
return false;
}
bool left = isCBT(root->left, 2*idx+1, totalNodes);
bool right = isCBT(root->right, 2*idx+2, totalNodes);
return left and right;
}
bool isMaxOrder(struct Node* root){
//leaf node
if(root->left==NULL and root->right==NULL){
return true;
}
//only left child exist
if(root->right==NULL){
return root->data > root->left->data;
}
else{ //both child exist
bool left = isMaxOrder(root->left);
bool right = isMaxOrder(root->right);
return left and right and ((root->data > root->left->data) and (root->data > root->right->data));
}
}
bool isHeap(struct Node* tree) {
int idx=0;
int totalNodes = countNodes(tree);
if(isCBT(tree,idx,totalNodes) and isMaxOrder(tree)){
return true;
}
else{
return false;
}
}
};