Maximum Depth of N-ary Tree

Problem Id: 559 Difficulty: Easy Tag: Tree Tag: Depth-first Search Tag: Breadth-first Search


Intuition

I use Depth-first Search in the solution of this problem. And it could just be solved recursivly.

Solution


"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if root is None:
            return 0
        if root.children:
            return max(map(self.maxDepth, root.children)) + 1
        return 1