题目

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

例如,给定一个 3叉树 :

我们应返回其最大深度,3

方案一(递归)

  1. """
  2. # Definition for a Node.
  3. class Node:
  4. def __init__(self, val=None, children=None):
  5. self.val = val
  6. self.children = children
  7. """
  8. def maxDepth(root: 'Node') -> int:
  9. if not root:
  10. return 0
  11. if not root.children:
  12. return 1
  13. depth = []
  14. for child in root.children:
  15. depth.append(self.maxDepth(child))
  16. return max(depth) + 1

原文

https://leetcode-cn.com/explore/learn/card/n-ary-tree/160/recursion/624/