forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
55 lines (41 loc) · 1.12 KB
/
main2.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// Source : https://leetcode.com/problems/check-completeness-of-a-binary-tree/
/// Author : liuyubobobo
/// Time : 2018-12-15
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isCompleteTree(TreeNode* root) {
if(!root) return true;
queue<pair<TreeNode*, int>> queue;
queue.push(make_pair(root, 1));
int max_index = -1, sz = 0;
while(!queue.empty()){
TreeNode* node = queue.front().first;
int index = queue.front().second;
queue.pop();
sz ++;
max_index = max(max_index, index);
if(node->left)
queue.push(make_pair(node->left, 2 * index));
if(node->right)
queue.push(make_pair(node->right, 2 * index + 1));
}
return sz == max_index;
}
};
int main() {
return 0;
}