304 North Cardinal St.
Dorchester Center, MA 02124

# Binary Tree Level Order Traversal LeetCode Solution

## Problem – Binary Tree Level Order Traversal LeetCode Solution

Given the `root` of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Example 1:

``````Input: root = [3,9,20,null,null,15,7]
Output: [,[9,20],[15,7]]``````

Example 2:

``````Input: root = 
Output: []``````

Example 3:

``````Input: root = []
Output: []``````

Constraints:

• The number of nodes in the tree is in the range `[0, 2000]`.
• `-1000 <= Node.val <= 1000`

### Binary Tree Level Order Traversal LeetCode Solution in Java

``````public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {

if(root == null) return wrapList;

queue.offer(root);
while(!queue.isEmpty()){
int levelNum = queue.size();
for(int i=0; i<levelNum; i++) {
if(queue.peek().left != null) queue.offer(queue.peek().left);
if(queue.peek().right != null) queue.offer(queue.peek().right);
}
}
return wrapList;
}
}
``````

### Binary Tree Level Order Traversal LeetCode Solution in C++

``````vector<vector<int>> ret;

void buildVector(TreeNode *root, int depth)
{
if(root == NULL) return;
if(ret.size() == depth)
ret.push_back(vector<int>());

ret[depth].push_back(root->val);
buildVector(root->left, depth + 1);
buildVector(root->right, depth + 1);
}

vector<vector<int> > levelOrder(TreeNode *root) {
buildVector(root, 0);
return ret;
}
``````

### Binary Tree Level Order Traversal LeetCode Solution in Python

``````def levelOrder(self, root):
ans, level = [], [root]
while root and level:
ans.append([node.val for node in level])
LRpair = [(node.left, node.right) for node in level]
level = [leaf for LR in LRpair for leaf in LR if leaf]
return ans
``````
##### Binary Tree Level Order Traversal LeetCode Solution Review:

In our experience, we suggest you solve this Binary Tree Level Order Traversal LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Binary Tree Level Order Traversal LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Binary Tree Level Order Traversal LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions