<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[3],[9,20],[15,7]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [[1]]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li>
	<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
</ul>

class Solution {
public:
    // void traverse(TreeNode* root, int n, vector<vector<int>>& ans){
    //     if(root == NULL) return;
    //     if (n >= ans.size()) {
    //     ans.push_back(vector<int>());
    //     }
    //     ans[n].push_back(root -> val);
    //     traverse(root -> left, n+1, ans);
    //     traverse(root -> right, n+1, ans);

    // }
    // void helper(queue<TreeNode*>& q,vector<int>& ans;){
         
    // }
    vector<vector<int>> levelOrder(TreeNode* root) {
        ios_base::sync_with_stdio(false);
        vector<vector<int>> ans;
        queue<TreeNode*> q;
        // using queue
        if(root) q.push(root);

    while(!q.empty()){
        int levelSize = q.size();

        ans.push_back(vector<int>());

        for (int i = 0; i < levelSize; i++) {
            TreeNode* temp = q.front();
            q.pop();

            ans.back().push_back(temp->val); 
            if(temp->left){
                q.push(temp->left);
            }
            if(temp->right){
                q.push(temp->right);
            }
        }
    }

    return ans;


    }
};
14700