# Binary Tree Level Order Traversal

## [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

&#x20;For example:\
&#x20;Given binary tree `[3,9,20,null,null,15,7]`,<br>

```

    3
   / \
  9  20
    /  \
   15   7
```

&#x20;return its level order traversal as:<br>

```

[
  [3],
  [9,20],
  [15,7]
]
```

## Solutions

### 🧠 Cpp

```cpp
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

#include <queue>
#include <utility>

class Solution
{

public:
    vector<vector<int>> levelOrder(TreeNode* root)
    {
        if(!root)
            return {};

        vector<vector<int>> res{};
        std::queue<TreeNode* >   q_to_process;
        q_to_process.push(root);

        do
        {
            //capture of this level values
            vector<int> level;
            //list of next level node to process on the next iteration
            std::queue<TreeNode*> q_next;

            for(TreeNode* node = q_to_process.front() ; !q_to_process.empty(); node = q_to_process.front())
            {
                q_to_process.pop();

                level.push_back(node->val);
                if(node->left)
                    q_next.push(node->left);
                if(node->right)
                    q_next.push(node->right);
            }

            res.push_back(std::move(level));
            q_to_process = std::move(q_next);

        }while(!q_to_process.empty());

        return res;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anton-veselskyi.gitbook.io/codding-problems-solutions/leetcode/medium/binary-tree-level-order-traversal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
