</>

102. Binary Tree Level Order Traversal

Medium NeetCode 150
Trees Time O(N) Space O(W)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Trees
Time
O(N)
Space
O(W)

Standard Solution (C++)

SummaryGiven the root of a binary tree, collect each node's value grouped by depth, returning a list of lists where each inner list contains all values at that level from left to right.

Approach
  • Use a BFS queue initialized with the root; each iteration processes exactly one level at a time.
  • Before dequeuing, snapshot the current queue size — this tells us exactly how many nodes belong to the current level.
  • Dequeue that many nodes, record their values, and enqueue their non-null children for the next pass.
  • Because BFS visits nodes in breadth-first order, each snapshot naturally corresponds to one complete level.
#include <vector>
#include <queue>
using namespace std;

// TreeNode definition (provided by LeetCode)
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* l, TreeNode* r) : val(x), left(l), right(r) {}
};

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

        queue<TreeNode*> q;
        q.push(root);

        while (!q.empty()) {
            // Snapshot the number of nodes at this level before expanding
            int levelSize = static_cast<int>(q.size());
            vector<int> level;
            level.reserve(levelSize);

            for (int i = 0; i < levelSize; ++i) {
                TreeNode* node = q.front();
                q.pop();
                level.push_back(node->val);

                // Enqueue children for the next level
                if (node->left)  q.push(node->left);
                if (node->right) q.push(node->right);
            }

            result.push_back(std::move(level));
        }

        return result;
    }
};
Time O(n) Space O(n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

/**
* Definition for a binary tree node.
* 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>> res;
        if(!root){
            return res;
        }
        queue<TreeNode*> q;
        q.push(root);

        vector<int> curLevel;
        while(!q.empty()){
            int levelSize=q.size();
            curLevel.clear();
            for(int i=0; i<levelSize; ++i){
                TreeNode* curNode=q.front();
                curLevel.push_back(curNode->val);
                if(curNode->left){
                    q.push(curNode->left);
                }
                if(curNode->right){
                    q.push(curNode->right);
                }
                q.pop();
            }
            res.push_back(curLevel);
        }
        return res;
    }
};

// Time Complexity: O(N)
// Space Complexity: O(W) for the queue, where W is the maximum width of the tree.
Time O(N) Space O(W)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern