# Definition for a binary tree node.
# 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]]
        """
        if root == None: return []
        
        stack = []
        level = 0
        results = {}
        while root != None or len(stack) > 0:
            if root != None:
                if not level in results:
                    results[level] = [root.val]
                else:
                    results[level].append(root.val)
                stack.append([root, level])
                root = root.left
                level += 1
            else:
                root, level = stack.pop()
                root = root.right
                level += 1
        return [results[key] for key in sorted(results.keys(), reverse=True)]