BiruLyu
5/30/2017 - 10:00 PM

## 102. Binary Tree Level Order Traversal(BFS).java

``````/**
* 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>> result = new ArrayList<>();
levelOrderDFS(root, result, 0);
return result;
}

private void levelOrderDFS(TreeNode root, List<List<Integer>> result, int level) {
if (root == null) {
return;
}
if (result.size() < level + 1) {
List<Integer> list = new ArrayList<>();
}
levelOrderDFS(root.left, result, level + 1);
levelOrderDFS(root.right, result, level + 1);
}
}``````
``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/

/*
1. BFS 每次explored一层，for循环将一层的节点的子节点都放入queue，每次for循环结束level++
2. BFS 将（TreeNode, Level）存入queue
3. DFS 递归
func(node, height)
func(node.left, heigh + 1)
func(node.right, heigh + 1)

*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null) return res;
queue.offer(root);
while(!queue.isEmpty()) {
int curLen = queue.size();
List<Integer> level = new ArrayList<Integer>();
for(int i = 0; i < curLen; i++) {
TreeNode temp = queue.poll();