💥🔥🎯Easiest Solution Python3 🐍✅ C ✅ Java ✅ C++ ✅ Python 🐍✅ detailed Explanation 🔥☠️
IntuitionIF U FIND MY SOLUTION HELPFUL FOR YOU THEN PLEASE CONSIDER THE UPVOTE .. IT HELPS OTHER USERS TO EASILY FIND MY SOLUTION AND THAT WILL BE A GREAT HELP.
# Intuition
![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/b6346e2b-628a-47a5-99d3-8e3d3e3a4f69_1713238884.0542805.jpeg)
# IF U FIND MY SOLUTION HELPFUL FOR YOU THEN PLEASE CONSIDER THE UPVOTE .. IT HELPS OTHER USERS TO EASILY FIND MY SOLUTION AND THAT WILL BE A GREAT HELP. THANKS GUY'S.
https://youtu.be/wf1gxazILj0
```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) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        
        if (!root)
            return {};
        vector<vector<int>> res;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            int n = q.size();
            vector<int> temp;
            for (int i = 0; i < n; i++) {
                TreeNode* node = q.front();
                q.pop();
                temp.push_back(node->val);
                if (node->left)
                    q.push(node->left);
                if (node->right)
                    q.push(node->right);
            }
            res.push_back(temp);
        }
        return res;
    }
};
```
```python3 []
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        res = []
        q = collections.deque()
        q.append(root)
        while q:
            q_len = len(q)
            level = []
            for i in range(q_len):
                node = q.popleft()
                if node is not None:
                    level.append(node.val)
                    q.append(node.left)
                    q.append(node.right)
            if level:
                res.append(level)
        return res
```
```Java []
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    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 n = q.size();
            List<Integer> temp = new LinkedList<>();
            for (int i = 0; i < n; i++) {
                TreeNode node = q.poll();
                temp.add(node.val);
                if (node.left != null)
                    q.add(node.left);
                if (node.right != null)
                    q.add(node.right);
            }
            res.add(temp);
        }
        return res;
    }
}
```
```C []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume
 * caller calls free().
 */

void trace_depth(struct TreeNode* n, int depth, int* cmp) {
    if (!n)
        return;
    if (++depth > *cmp)
        *cmp = depth;
    trace_depth(n->left, depth, cmp);
    trace_depth(n->right, depth, cmp);
}

void trace(struct TreeNode* n, int depth, int** a, int* id) {
    if (!n)
        return;

    if (!id[depth])
        a[depth] = (int*)malloc(sizeof(int) * ((depth > 10) ? 1024 : (1 << depth)));
    a[depth][id[depth]++] = n->val;
    trace(n->left, depth + 1, a, id);
    trace(n->right, depth + 1, a, id);
}

int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    *returnSize = 0;
    if (!root)
        return NULL;

    trace_depth(root, 0, returnSize);
    int** result = (int**)malloc(sizeof(int*) * (*returnSize));
    *returnColumnSizes = (int*)calloc(*returnSize, sizeof(int));
    trace(root, 0, result, *returnColumnSizes);

    return result;
}
```
```python []
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []
        q = collections.deque()
        q.append(root)
        while q:
            q_len = len(q)
            level = []
            for i in range(q_len):
                node = q.popleft()
                if node is not None:
                    level.append(node.val)
                    q.append(node.left)
                    q.append(node.right)
            if level:
                res.append(level)
        return res
```


<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    O(n) -- no. of nodes
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(n)
# Code
```
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []
        q = collections.deque()
        q.append(root)
        while q:
            q_len = len(q)
            level = []
            for i in range(q_len):
                node = q.popleft()
                if node is not None:
                    level.append(node.val)
                    q.append(node.left)
                    q.append(node.right)
            if level:
                res.append(level)
        return res
```
# Please upvote if it's useful for you
![th.jpeg](https://assets.leetcode.com/users/images/a80db95b-3348-441f-9395-bf7e84831fa9_1713239035.693936.jpeg)

['Tree', 'Breadth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3']