Skip to content

Commit b91dd11

Browse files
Create check_completeness_of_binary_tree.cpp
1 parent 0ab388c commit b91dd11

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

check_completeness_of_binary_tree.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
bool isCompleteTree(TreeNode* root) {
15+
if(!root) {
16+
return true;
17+
}
18+
// ok so what i have to do(hopefully) is a level order traversal
19+
20+
queue<TreeNode *> qu;
21+
TreeNode *current = root;
22+
qu.push(current);
23+
24+
bool isBoth = true; //checks is a node has both children or not
25+
26+
while(!qu.empty()) {
27+
28+
current = qu.front();
29+
qu.pop();
30+
31+
32+
if(current->left) {
33+
34+
if(!isBoth) return false;
35+
qu.push(current->left);
36+
37+
}
38+
else isBoth = false;
39+
40+
41+
if(current->right) {
42+
43+
if(!isBoth) return false;
44+
qu.push(current->right);
45+
46+
}
47+
else isBoth = false;
48+
}
49+
return true;
50+
}
51+
};

0 commit comments

Comments
 (0)