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).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
if(root == null)
return res;
LinkedList<TreeNode> curr = new LinkedList<>();
LinkedList<TreeNode> next = new LinkedList<>();
List<Integer> list = new ArrayList<Integer>();
curr.offer(root);
while(curr.size() > 0) {
TreeNode temp = curr.poll();
list.add(temp.val);
if(temp.left != null)
next.add(temp.left);
if(temp.right != null)
next.add(temp.right);
if(curr.size() == 0) {
curr = next;
next = new LinkedList<>();
res.add(0, list);
list = new ArrayList<>();
}
}
return res;
}
}