Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Binary Tree Level Order Traversal problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Binary Tree Level Order Traversal problem solution, we have given the root of a binary tree, return the level order traversal of its nodes’ values.

Leetcode Binary Tree Level Order Traversal problem solution

Leetcode Binary Tree Level Order Traversal problem solution in Python.

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return None
        
        queue = [root]
        result = []
        while queue:
            
            arr = []
            for i in range(len(queue)):
                node = queue.pop(0)
                arr.append(node.val)
                left = node.left
                right = node.right
                
                if left:
                    queue.append(left)
                if right:
                    queue.append(right)
                    
            result.append(arr)

Binary Tree Level Order Traversal problem solution in Java.

public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> q = new LinkedList();
        q.add(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> level = new ArrayList<>();
            while(size > 0){
                TreeNode node = q.poll();
                level.add(node.val);
                if(node.left != null){
                    q.add(node.left);
                }
                if(node.right != null){
                    q.add(node.right);
                }
                size--;
            }
            res.add(level);
        }
        return res;
    }

Problem solution in C++.

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode *> q;
        vector<vector<int>> v;
        vector<int> tmp;
        if (!root) return {};
        tmp.push_back(root->val); q.push(root); v.push_back(tmp);
        while (!q.empty())
        {
            int size = q.size();
            vector<int> tmp;
            for (int i = 0; i < size; i++)
            {
                TreeNode *n = q.front(); q.pop();
                //cout << n->val << endl;
                if (n->left)
                {
                    tmp.push_back(n->left->val); 
                    q.push(n->left);
                }
                if (n->right)
                {
                    tmp.push_back(n->right->val); 
                    q.push(n->right);
                }
            }
            if (!tmp.empty()) v.push_back(tmp);
        }
        return v;
    }
};

Problem solution in C.

void addnum(int size, int **array, int val, int level)
{
    array[level] = realloc(array[level], size * sizeof (int));
    array[level][size-1] = val;
    
}

int maxdepth(struct TreeNode* root) {
    if (root == NULL) {
        return (0);
    }
    int ldepth = maxdepth(root->left) + 1;
    int rdepth = maxdepth(root->right) + 1;
    
    if (ldepth > rdepth)
        return ldepth;
    else
        return rdepth;
}

void currentlevel(struct TreeNode *root, int *returnColumnSizes, int **array, int level)
{
    if (root == NULL) 
    {
        return;
    }
    returnColumnSizes[level] += 1;
    addnum(returnColumnSizes[level], array, root->val, level);
    level++;
    currentlevel(root->left, returnColumnSizes, array, level);
    currentlevel(root->right, returnColumnSizes, array, level);    
}			


int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    int depth, **array;
    depth = maxdepth(root);
    *returnSize = depth;
    *returnColumnSizes = calloc(depth, sizeof(int));
    array = calloc(depth, sizeof (int *));
    currentlevel(root, *returnColumnSizes, array, 0);
    return (array);

}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes