# 107. Binary Tree Level Order Traversal II

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/

> 内容描述

```
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,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
return its bottom-up level order traversal as:
[
  [15,7],
  [9,20],
  [3]
]
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******


用102 的算法作弊，递归


```python
class Solution:
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        def dfs(node, level, res):
            if not node:
                return
            if len(res) < level:
                res.append([])
            res[level-1].append(node.val)
            dfs(node.left, level+1, res)
            dfs(node.right, level+1, res)  
                
        res = []
        dfs(root, 1, res)
        return res[::-1]
```

> 思路 2
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******

迭代

用102 的算法作弊

```python
class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        cur_level = [root]
        res = []
        while cur_level:
            nxt_level = []
            tmp_res = []
            for node in cur_level:
                if node.left:
                    nxt_level.append(node.left)
                if node.right:
                    nxt_level.append(node.right)
                tmp_res.append(node.val)
            cur_level = nxt_level
            res.append(tmp_res)
        return res[::-1]
```

