---
description: Tree BFS
---

# Binary Tree Level Order Traversal II (Reverse Level Order Traversal)

## Problem

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).

{% hint style="info" %}
For example:

Given binary tree `[3,9,20,null,null,15,7]`,\


```
    3
   / \
  9  20
    /  \
   15   7
```

return its bottom-up level order traversal as:\


```
[
  [15,7],
  [9,20],
  [3]
]
```
{% endhint %}

## Solution

```
class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root is None:
            return []
        
        result = deque()
        queue = deque()
        queue.append(root)
        while queue:
            currentLevel = []
            levelSize = len(queue)
            for _ in range(levelSize):
                currentNode = queue.popleft()
                currentLevel.append(currentNode.val)
                
                if currentNode.left:
                    queue.append(currentNode.left)
                if currentNode.right:
                    queue.append(currentNode.right)
            result.appendleft(currentLevel)
        return result
        
#Time: O(n)
#Space: O(n)
```

