1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
check-completeness-of-a-binary-tree.cpp 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(n)
// Space: O(w)
/**
* 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) {
bool end = false;
vector<TreeNode*> current{root};
while (current.empty() == false) {
vector<TreeNode*> next_level;
for (const auto& node : current) {
if (!node) {
end = true;
continue;
}
if (end) {
return false;
}
next_level.emplace_back(node->left);
next_level.emplace_back(node->right);
}
current = move(next_level);
}
return true;
}
};
// Time: O(n)
// Space: O(w)
class Solution2 {
public:
bool isCompleteTree(TreeNode* root) {
vector<pair<TreeNode*, int>> prev_level, current{{root, 1}};
int count = 0;
while (current.empty() == false) {
count += current.size();
vector<pair<TreeNode*, int>> next_level;
for (const auto& node : current) {
if (node.first) {
next_level.emplace_back(node.first->left, 2 * node.second);
next_level.emplace_back(node.first->right, 2 * node.second + 1);
}
}
prev_level = move(current);
current = move(next_level);
}
return prev_level.back().second == count;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助