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);

}