leetcode-559. N 叉树的最大深度

559. N 叉树的最大深度 - 力扣(Leetcode)

dfs

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

func maxDepth(root *Node) int {
    if root == nil {
        return 0
    }

    maxSubDepth := 0

    for _, v := range root.Children {
        if v == nil {
            continue
        }
        subDepth := maxDepth(v)
        if subDepth > maxSubDepth {
            maxSubDepth = subDepth
        }
    }

    return maxSubDepth + 1
}
posted @ 2022-12-31 20:27  吴丹阳-V  阅读(13)  评论(0编辑  收藏  举报