Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if (!root) {
return res;
}
queue<Node*> q;
q.emplace(root);
while (!empty(q)) {
vector<int> line;
int n = size(q);
for (int i = 0; i < n; ++i) {
Node* t = q.front();
q.pop();
line.emplace_back(t->val);
for (auto& x : t->children) {
q.emplace(x);
}
}
res.emplace_back(line);
}
return res;
}
};