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},

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its level order traversal as:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]

BFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/

public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>(); // result list
List<Integer> sub; // sub list for each level
if (root == null){return res;}

Queue<TreeNode> level = new LinkedList<>();
level.add(root);

while (level.size() > 0){
sub = new ArrayList<>();
int levelNum = level.size();

for (int i = 0; i < levelNum; i++){
TreeNode cur = level.poll();
if (cur.left != null) level.add(cur.left);
if (cur.right != null) level.add(cur.right);
sub.add(cur.val);
}
res.add(sub);
}
return res;
}
}

DFS

```java
public class Solution {
public List> levelOrder(TreeNode root) {
List> res = new ArrayList>();
levelHelper(res, root, 0);
return res;
}

public void levelHelper(List<List<Integer>> res, TreeNode root, int height) {
    if (root == null) return;
    if (height >= res.size()) {
        res.add(new LinkedList<Integer>());
    }
    res.get(height).add(root.val);
    levelHelper(res, root.left, height+1);
    levelHelper(res, root.right, height+1);
}

}
```A