Skip to content

Commit 21dc212

Browse files
authored
Create main.cpp
1 parent f95a370 commit 21dc212

File tree

1 file changed

+33
-0
lines changed
  • 19 - Heap Data Structure Problems/04 - Check Completeness of a Binary Tree

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int totalCount(TreeNode* root){
15+
if(root == NULL) return 0;
16+
17+
return 1 + totalCount(root -> left) + totalCount(root -> right);
18+
}
19+
int isCBT(TreeNode* root, int index, int count){
20+
if(root == NULL) return true;
21+
if(index >= count) return false;
22+
23+
bool left = isCBT(root -> left, 2 * index + 1, count);
24+
bool right = isCBT(root -> right, 2 * index + 2, count);
25+
26+
return left && right;
27+
}
28+
bool isCompleteTree(TreeNode* root) {
29+
int index = 0;
30+
int count = totalCount(root);
31+
return isCBT(root, index, count);
32+
}
33+
};

0 commit comments

Comments
 (0)