2025-07-30 12:53:02 +0000 UTC
Maximum Depth of N-ary Tree
Categories:
Links
Code
"""
# Definition for a Node.
class Node:
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
def dfs(node: Node) -> int:
if not node:
return 0
if not node.children:
return 1
return 1 + max(dfs(child) for child in node.children)
return dfs(root)