# My Java 2 solutions 1ms(dfs) & 2ms(bfs)

• ``````​/**
* 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<List<Integer>>();
if (root == null)
return res;
q.offer(root);
while (!q.isEmpty()) {
List<Integer> tmp = new ArrayList<Integer>();
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode t = q.poll();
if (t.left != null)
q.offer(t.left);
if (t.right != null)
q.offer(t.right);
}
}
return res;
}
}

/**
* 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<List<Integer>>();
dfs(res,root,0);
return res;
}
public void dfs(List<List<Integer>> list,TreeNode node,int deep){
if(node==null)return;
if(list.size()==deep)