添加 problem 107

This commit is contained in:
YDZ
2019-05-16 08:46:55 +08:00
parent 3665185618
commit 00d0bca8e7
3 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package leetcode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func levelOrderBottom(root *TreeNode) [][]int {
tmp := levelOrder(root)
res := [][]int{}
for i := len(tmp) - 1; i >= 0; i-- {
res = append(res, tmp[i])
}
return res
}

View File

@@ -0,0 +1,54 @@
package leetcode
import (
"fmt"
"testing"
)
type question107 struct {
para107
ans107
}
// para 是参数
// one 代表第一个参数
type para107 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans107 struct {
one [][]int
}
func Test_Problem107(t *testing.T) {
qs := []question107{
question107{
para107{[]int{}},
ans107{[][]int{}},
},
question107{
para107{[]int{1}},
ans107{[][]int{[]int{1}}},
},
question107{
para107{[]int{3, 9, 20, NULL, NULL, 15, 7}},
ans107{[][]int{[]int{15, 7}, []int{9, 20}, []int{3}}},
},
}
fmt.Printf("------------------------Leetcode Problem 107------------------------\n")
for _, q := range qs {
_, p := q.ans107, q.para107
fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", levelOrderBottom(root))
}
fmt.Printf("\n\n\n")
}

View File

@@ -0,0 +1,35 @@
# [107. Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)
## 题目
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],
```c
3
/ \
9 20
/ \
15 7
```
return its bottom-up level order traversal as:
```c
[
[15,7],
[9,20],
[3]
]
```
## 题目大意
按层序从下到上遍历一颗树。用一个队列即可实现。