- categories: Code, Interview Question, leetcode, Easy
- source: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/
- topics: Tree
Given a n-ary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root:
return 0
depth = 1
children = root.children
next_children = list([])
while children:
depth += 1
next_children = []
for c in children:
next_children.extend(c.children)
children = next_children
return depth