leetcode-589. N 叉树的前序遍历

589. N 叉树的前序遍历 - 力扣(Leetcode)

Go语言的切片操作方便性还不错

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Children []*Node
 * }
 */

func preorder(root *Node) []int {
    if root == nil {
        return []int{}
    }

    curSeq := []int{root.Val}
    for _, v := range root.Children {
        if v != nil {
            subSeq := preorder(v)
            curSeq = append(curSeq, subSeq...)
        }
    }

    return curSeq
}
posted @ 2023-01-01 12:35  吴丹阳-V  阅读(14)  评论(0)    收藏  举报