Advertisement

102. 二叉树的层次遍历(Go语言实现)

阅读量:

题目描述

对于一棵二叉树,要求按照层次顺序输出其所有节点的值。也就是说,需按照从上到下、每一层由左至右的顺序依次访问节点。

例如:
若输入的二叉树结构为 [3,9,20,null,null,15,7],则其结构可表示为:

3
/ \
9 20
/ \
15 7

该二叉树对应的层次遍历结果应为:

[
[3],
[9,20],
[15,7]
]

代码实现

复制代码
 /** * Definition for a binary tree node.
    
  * type TreeNode struct {
    
  *     Val int
    
  *     Left *TreeNode
    
  *     Right *TreeNode
    
  * }
    
  */
    
 func levelOrder(root *TreeNode) [][]int {
    
     res := make([][]int, 0)
    
     if root == nil {
    
     return res
    
     }
    
     queue := make([]map[int]*TreeNode, 0)
    
  
    
     i

全部评论 (0)

还没有任何评论哟~