Skip to content

Latest commit

 

History

History
24 lines (23 loc) · 526 Bytes

面试题32 - I. 从上到下打印二叉树.md

File metadata and controls

24 lines (23 loc) · 526 Bytes
func levelOrder(root *TreeNode) []int {
    if root == nil {
        return nil
    }
    res := []int{}
    queue := []*TreeNode{root}
    for len(queue) != 0 {
        temp := []*TreeNode{}
        for _, node := range queue {
            res = append(res, node.Val)
            if node.Left != nil {
                temp = append(temp, node.Left)
            }
            if node.Right != nil {
                temp = append(temp, node.Right)
            }
        }
        queue = temp
    }
    return res
}