# leetcode102.二叉树的层序遍历

```
func levelOrder(root *TreeNode) [][]int {
    if root==nil{
        return [][]int{}
    }
    nodes:=make([]*TreeNode,0) //用于存储每一层的node,遍历后清空并存储下一层的node
    nodes=append(nodes,root)
    res:=make([][]int,0)
    
    for len(nodes)>0{
        currentNodeList:=nodes[:]
        nodes=make([]*TreeNode,0)
        levelVal:=make([]int,0) //用于存储遍历出的层级节点val
        for _,v:=range currentNodeList{
            levelVal=append(levelVal,v.Val)
            
            if v.Left!=nil{
                nodes=append(nodes,v.Left)
            }
            
            if v.Right!=nil{
                nodes=append(nodes,v.Right)
            }
        }
        
        res=append(res,levelVal)
        
    }
    
    return res
}
```
