1.DFS

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

 

2.BFS

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        now,level=[root],1
        while now:
            now=[child for leaf in now for child in leaf.children if child]
            level+=1 if now else 0
        return level
                

 

相关文章:

  • 2021-04-27
  • 2021-07-14
  • 2021-05-04
  • 2021-12-01
  • 2021-08-10
  • 2021-12-12
猜你喜欢
  • 2021-10-15
  • 2021-09-17
  • 2021-07-18
  • 2022-12-23
  • 2021-12-06
  • 2021-10-18
  • 2021-10-05
相关资源
相似解决方案