今天有些仓促,只刷了一道题目,关于输入根节点来输出n叉树的最大深度。算法本身不难理解,简单的一个递归而已。但那种题目的写法让我有点苦恼,为此我专门查阅了资料。现给出python写法的基本框架解释:
def f(text:str,max_len:'int>0'=80) ->str:
"""这个是函数的帮助说明文档,help时会显示"""
return True
"""
函数声明中,text:str
text 是参数 :冒号后面 str是参数的注释。
如果参数有默认值,还要给注释,如下写。
max_len:'int>0'=80
->str 是函数返回值的注释。
这些注释信息都是函数的元信息,保存在f.__annotations__字典中、
需要注意,python对注释信息和f.__annotations__的一致性,不做检查
不做检查,不做强制,不做验证!什么都不做。
"""
详情见https://blog.csdn.net/yqyangcyq/article/details/105611579
题目详情
题目的代码`
"""
# 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') -> int:
if not root:
return 0
d = 1
for child in root.children:
d = max(d, self.maxDepth(child) + 1)
return d