diff --git a/cpp/2207/220713.cpp b/cpp/2207/220713.cpp new file mode 100644 index 0000000..4d9c478 --- /dev/null +++ b/cpp/2207/220713.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + explicit TreeNode(int x, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {} +}; + + +class Solution { +public: + std::vector> levelOrder(TreeNode* root) { + if (!root) + return {}; + std::vector> ret {{}}; + std::queue> q; + q.push({root, 0}); + int cl = 0; + while (!q.empty()) { + const auto [v, l] = q.front(); + q.pop(); + if (l > cl) { + ++cl; + ret.emplace_back(); + } + ret.back().push_back(v->val); + if (v->left) + q.push({v->left, 1 + l}); + if (v->right) + q.push({v->right, 1 + l}); + } + return ret; + } +}; \ No newline at end of file