把二叉树打印成多行
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
func Print(root *TreeNode) [][]int { if root == nil { return [][]int{} } queue := []*TreeNode{root} res := [][]int{} for len(queue) != 0 { curRes := []int{} newQueue := []*TreeNode{} for _, node := range queue { curRes = append(curRes, node.Val) if node.Left != nil { newQueue = append(newQueue, node.Left) } if node.Right != nil { newQueue = append(newQueue, node.Right) } } res = append(res, curRes) queue = newQueue } return res }