# Binary Tree Level Order Traversal Problem & Solution

## C++ Solution

``````#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

static const int _=[]{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

/**
* struct TreeNode {
*   int val;
*   TreeNode *left;
*   TreeNode *right;
*   TreeNode() : val(0), left(nullptr), right(nullptr) {}
*   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> results;

help(root, results, 1);

return results;
}

private:
void help(TreeNode* root, vector<vector<int>>& results, int level) {
if (root == nullptr) {
return;
}

if (results.size() < level) {
results.push_back({});
}

results[level - 1].push_back(root->val);

help(root->left, results, level + 1);
help(root->right, results, level + 1);
}
};
``````

## Start Here

Many paths, there are. Follow yours, you must.