题解 | #判断是不是完全二叉树#
判断是不是完全二叉树
https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: bool isCompleteTree(TreeNode* root) { queue<TreeNode*>q; bool flag = false; q.push(root); TreeNode*cur; while(!q.empty()) { for(int i = 0;i<q.size();i++) { cur = q.front(); q.pop(); if(cur == NULL) { flag = true; //大体上跟层序遍历的类似,这里就需要判断如果后面有节点,并且前面是空的节点,这里就需要用到if else先把flag设定成true,然后再是当下一次cur不等于null的时候返回false } else{ if(flag) return false; q.push(cur->left); q.push(cur->right); } } } return true; } };