Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Binary Tree Level Order Traversal problem solution

YASH PAL, 31 July 2024

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

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)

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes