class Solution { public: bool isCompleteTree(TreeNode* root) { queue<TreeNode*> node_q; node_q.push(root); bool expect_null = false; while (!node_q.empty()) { if (node_q.front() == nullptr) expect_null = true; else { if (expect_null) return false; else { node_q.push(node_q.front()->left); node_q.push(node_q.front()->right); } } node_q.pop(); } return true; } };
观察易得,完全二叉树的层序遍历一定是(全是节点 + 全是NULL)的样式。
判断层序遍历结果即可。