Contents

# 107. Binary Tree Level Order Traversal II

## 題目 :

Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).

### Example :

Note

Example 1:

Input: root = [3,9,20,null,null,15,7] Output: [[15,7],[9,20],]

Example 2:

Input: root =  Output: []

Example 3:

Input: root = [] Output: []

#### 解題思路 :

• 這題可以直接參考 leetcode102題解，使用到的觀念一模一樣，只是多一個反轉結果而已!!!

### Queue解法

• time complexity: O(n) , space complexity: O(n)

Runtime: 0 ms, faster than 100.00% of Go online submissions for Binary Tree Level Order Traversal II.

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 `````` ``````/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func levelOrderBottom(root *TreeNode) [][]int { var res [][]int if root == nil { return res } var queue []*TreeNode queue = append(queue, root) for len(queue) != 0 { length := len(queue) tempArr := make([]int,0) for i := 0; i < length; i++ { node := queue if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } tempArr = append(tempArr, node.Val) queue = queue[1:] } res = append(res, tempArr) } for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 { res[i], res[j] = res[j], res[i] } return res } ``````