LeetCode 958. 二叉树的完全性检验
原题链接
中等
作者:
linux_2019
,
2019-09-23 10:13:40
,
所有人可见
,
阅读 633
C++ 代码
/**
* 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<TreeNode *> q;
q.push(root);
bool end=0;
while(q.size())
{
int n=q.size();
for(int i=0;i<n;i++)
{
auto t=q.front();
q.pop();
if(!t) {end=1;continue;}
if(end)
return false;
q.push(t->left);
q.push(t->right);
}
}
return true;
}
};