# LeetCode – Binary Tree Level Order Traversal (Java)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},

```    3
/ \
9  20
/  \
15   7
```

return its level order traversal as [[3], [9,20], [15,7]]

Analysis

It is obvious that this problem can be solve by using a queue. However, if we use one queue we can not track when each level starts. So we use two queues to track the current level and the next level.

Java Solution

```public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> nodeValues = new ArrayList<Integer>(); if(root == null) return al;   LinkedList<TreeNode> current = new LinkedList<TreeNode>(); LinkedList<TreeNode> next = new LinkedList<TreeNode>(); current.add(root);   while(!current.isEmpty()){ TreeNode node = current.remove();   if(node.left != null) next.add(node.left); if(node.right != null) next.add(node.right);   nodeValues.add(node.val); if(current.isEmpty()){ current = next; next = new LinkedList<TreeNode>(); al.add(nodeValues); nodeValues = new ArrayList(); }   } return al; }```
Category >> Algorithms >> Interview
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• Peeyush Chandel

Another One queue solution.

``` public class Solution { public List<List> levelOrder(TreeNode root) { TreeNode tmp = new TreeNode(Integer.MIN_VALUE); List<List> result = new ArrayList<List>(); if(root == null){ return result; }else{ Queue queue = new LinkedList(); queue.add(root); queue.add(tmp); while(!queue.isEmpty() && queue.peek() != tmp){ List r = new ArrayList(); while(queue.peek() != tmp){ TreeNode t = queue.poll(); r.add(t.val); if(t.left!=null){ queue.add(t.left); } if(t.right!=null){ queue.add(t.right); } ```

``` } queue.poll(); queue.add(tmp); result.add(r); } ```

``` } return result; } } ```

• Randman Null

Dont need 2 queues. One is enough.

Key is to have a good exit condition.

``` public List<List> levelOrder(TreeNode root) { List<List> list = new ArrayList<List>(); List l1 = new ArrayList();```

``` if(null == root){ return list; } Queue q = new LinkedList(); q.offer(root); q.offer(null); while(!q.isEmpty()){ TreeNode top = q.poll(); if(null != top){ l1.add(top.val); if(null != top.left){ q.offer(top.left); } if(null != top.right ){ q.offer(top.right); } }else{ list.add(l1); l1 = new ArrayList(); q.offer(null); } ```

``` if (null == top && null == q.peek()){ break; } } return list; } ```

• Venu Bp

Another solution with one queue

public List<List> levelOrder(TreeNode root) {
List<List> results = new ArrayList<List>();
if(root == null)
return results;

List levelResult = new ArrayList();

queue.offer(root);
queue.offer(null);

TreeNode node = null;
while(! queue.isEmpty()){
node = queue.poll();
if(node != null){
}
if(queue.size() > 0 && node == null){
levelResult = new ArrayList();
queue.offer(null);
}
if(node!=null && node.left != null){
queue.offer(node.left);
}

if(node != null && node.right != null){
queue.offer(node.right);
}
}
return results;
}

• Burhan COKCA

Solution with one queue only.

public List<List> levelOrder(TreeNode root) {

//queue will handle this
List<List> result = new ArrayList();
if(root == null) return result;

List list = new ArrayList();

TreeNode rightMost = root;
//this is for the case that the right most doesnt have child nodes, so we need to keep track of the
TreeNode b = null;

while(!q.isEmpty()){
TreeNode t = q.remove();

if(t == rightMost){

list = new ArrayList();
if(t.right != null) rightMost = t.right;
else if(t.left != null) rightMost = t.left;
else rightMost = b;

}

if(t.left != null){