Friday, February 22, 2019

LeetCode 559. Maximum Depth of N-ary 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.
'''
"""

# Definition for a Node.
class Node:
    def __init__(self, val, children):
        self.val = val
        self.children = children
"""

class Solution:
    def maxDepth(self, root: 'Node') -> 'int':
        if not root:
            return 0
        elif not root.children:
            return 1
        else:
            return 1 + max([self.maxDepth(i) for i in root.children])

No comments:

Post a Comment