Skip to content

Commit e3218bb

Browse files
authored
Create main.cpp
1 parent be4fcbc commit e3218bb

File tree

1 file changed

+42
-0
lines changed
  • 19 - Heap Data Structure Problems/05 - Is Binary Tree Heap

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int countNodes(struct Node* root){
4+
if(root == NULL) return 0;
5+
6+
return 1 + countNodes(root -> left) + countNodes(root -> right);
7+
}
8+
9+
bool isCBT(struct Node* root, int index, int count){
10+
if(root == NULL) return true;
11+
12+
if(index >= count) return false;
13+
14+
else {
15+
bool left = isCBT(root -> left, 2 * index + 1, count);
16+
bool right = isCBT(root -> right, 2 * index + 2, count);
17+
18+
return left && right;
19+
}
20+
}
21+
22+
bool isMaxOrder(struct Node* root){
23+
if(root -> left == NULL && root -> right == NULL) return true;
24+
25+
if(root -> right == NULL) return (root -> data > root -> left -> data);
26+
else {
27+
bool left = isMaxOrder(root -> left);
28+
bool right = isMaxOrder(root -> right);
29+
30+
return (left && right &&
31+
(root -> data > root -> left -> data && root -> data > root -> right -> data));
32+
}
33+
}
34+
bool isHeap(struct Node* root) {
35+
int index = 0;
36+
int totalCount = countNodes(root);
37+
38+
if(isCBT(root, index, totalCount) && isMaxOrder(root)) return true;
39+
40+
return false;
41+
}
42+
};

0 commit comments

Comments
 (0)