-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path958.cpp
More file actions
32 lines (32 loc) · 933 Bytes
/
Copy path958.cpp
File metadata and controls
32 lines (32 loc) · 933 Bytes
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
class Solution {
public:
bool isCompleteTree(TreeNode *root) {
if (!root)
return true;
queue<TreeNode *> que;
que.push(root);
int size = 1;
bool finished = false;
while (!que.empty()) {
while (size > 0) {
root = que.front();
que.pop();
if (finished && (root->left || root->right))
return false;
if (root->left && root->right) {
que.push(root->left);
que.push(root->right);
} else if (root->left) {
que.push(root->left);
finished = true;
} else if (root->right)
return false;
else
finished = true;
--size;
}
size = que.size();
}
return true;
}
};