# Binary Tree Level Order Traversal II

Given a binary tree, return thebottom-up level ordertraversal 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]
]
```

从底部层序遍历其实还是从顶部开始遍历，只不过存储的方式有所改变,把level每次加到result的最前边：

```
result.insert(0,[i.val for i in level])
```

```
class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        result =[]
        level = [root]
        while level:
            newlevel = []
            result.insert(0,[i.val for i in level])
            for n in level:
                if n.left:
                    newlevel.append(n.left)
                if n.right:
                    newlevel.append(n.right)
            level = newlevel
        return result
```

或者最后返回result的reverse

* return result\[::-1]
* return list(reversed(result))

Note that`reversed(...)`does not return a list. You can get a reversed list using`list(reversed(array))`

.

```
class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        result =[]
        level = [root]
        while level:
            newlevel = []
            result.append([i.val for i in level])
            for n in level:
                if n.left:
                    newlevel.append(n.left)
                if n.right:
                    newlevel.append(n.right)
            level = newlevel
        return result[::-1]
```
