luoheng
10/7/2019 - 7:03 AM

levelOrderBottom

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func levelOrderBottom(root *TreeNode) [][]int {
    if root == nil {
        return nil
    }
    result := make([][]int, 0, 4)
    cur := []*TreeNode{root}
    var next []*TreeNode
    var temp []int
    for len(cur) != 0 {
        next, temp = make([]*TreeNode, 0, 4), make([]int, 0, 4)
        for _, node := range cur {
            if node != nil {
                temp = append(temp, node.Val)
                next = append(next, node.Left, node.Right)
            }
        }
        if len(temp) != 0 {
            result = append(result, temp)   
        }
        cur = next
    }
    for i := 0; i < len(result)/2; i++ {
        result[i], result[len(result)-i-1] = result[len(result)-i-1], result[i]
    }
    return result
}