File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
19 - Heap Data Structure Problems/05 - Is Binary Tree Heap Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments