BiruLyu
5/30/2017 - 10:18 PM

## 107. Binary Tree Level Order Traversal II(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>> levelOrderBottom(TreeNode root) {
return list;
}

if (node == null) return;
}

}``````
``````
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
res = [];
if not root:
return res;

res.append([root.val]);
frontier1 = [root];
while(len(frontier1) != 0):
frontier2 = [];
temp = [];
for i in frontier1: #######Explore all the node at current level
v = i;
if v.left:
frontier2.append(v.left);
temp.append(v.left.val);
if v.right:
frontier2.append(v.right);
temp.append(v.right.val);
if temp:
res.append(temp);
frontier1 = frontier2;

return res[::-1]; ###reverse the result list
``````
``````/**
* 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>> levelOrderBottom(TreeNode root) {
if (root == null) return res;
queue.offer(root);
while (!queue.isEmpty()) {
int curLen = queue.size();