## Binary Tree Level Order Traversal II

### 描述

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

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

    3
/ \
9  20
/  \
15   7


return its bottom-up level order traversal as:

[
[15,7]
[9,20],
[3],
]


### 递归版

// Binary Tree Level Order Traversal II
// 递归版，时间复杂度O(n)，空间复杂度O(n)
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
traverse(root, 1, result);
Collections.reverse(result);
return result;
}

void traverse(TreeNode root, int level,
List<List<Integer>> result) {
if (root == null) return;

if (level > result.size())

traverse(root.left, level+1, result);
traverse(root.right, level+1, result);
}
}


### 迭代版

// Binary Tree Level Order Traversal II
// 迭代版，时间复杂度O(n)，空间复杂度O(1)
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();

if(root == null) {
return result;
} else {
current.offer(root);
}

while (!current.isEmpty()) {
ArrayList<Integer> level = new ArrayList<>(); // elments in one level
while (!current.isEmpty()) {
TreeNode node = current.poll();
}
// swap
Queue<TreeNode> tmp = current;
current = next;
next = tmp;
}
Collections.reverse(result);
return result;
}
}