559. Maximum Depth of N-ary Tree
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 73% Topics: Tree, Depth-First Search, Breadth-First Search
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(h)
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root:
return 0
depth = 0
for child in root.children:
depth = max(depth, self.maxDepth(child))
return 1+depth
Solution from kamyu104/LeetCode-Solutions · MIT