add: 220408-CN [cpp]
This commit is contained in:
parent
2251e072ea
commit
383c665dce
|
|
@ -0,0 +1,43 @@
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Definition for a Node.
|
||||||
|
*/
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int val;
|
||||||
|
std::vector<Node*> children;
|
||||||
|
|
||||||
|
explicit Node(int _val = 0, std::vector<Node*> _children = {}) : val(_val), children(std::move(_children)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static std::vector<std::vector<int>> levelOrder(const Node* root) {
|
||||||
|
if (!root)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::queue<std::pair<const Node*, int>> q;
|
||||||
|
std::vector<std::vector<int>> ret = {{}};
|
||||||
|
|
||||||
|
int currentDepth = 0;
|
||||||
|
q.push({root, 0});
|
||||||
|
while (!q.empty()) {
|
||||||
|
auto n = q.front();
|
||||||
|
q.pop();
|
||||||
|
|
||||||
|
if (n.second > currentDepth)
|
||||||
|
ret.emplace_back();
|
||||||
|
currentDepth = n.second;
|
||||||
|
|
||||||
|
ret.back().push_back(n.first->val);
|
||||||
|
|
||||||
|
for (const auto* ch : n.first->children)
|
||||||
|
q.push({ch, 1 + n.second});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2204)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2204 220407.cpp)
|
ADD_EXECUTABLE(2204 220408-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue