소프트웨어에 대한 모든 것

LeetCode 풀기 - 559. Maximum Depth of N-ary Tree 본문

알고리즘/LeetCode

LeetCode 풀기 - 559. Maximum Depth of N-ary Tree

앤테바 2021. 12. 31. 07:10
반응형

559. Maximum Depth of N-ary Tree

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/

 

Maximum Depth of N-ary Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1) recursive

"""
# 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', depth=0) -> int:
        if root is None: return depth
        
        depth += 1
        cur_depth = depth
        for child in root.children:
            depth = max(depth, self.maxDepth(child, cur_depth))
        return depth

 

 

반응형
Comments