我正在解决 leetcode 上的一些问题,但我不明白他们编写的相同代码。比如为什么有一个箭头指向int(->int:)?我也不明白 TreeNode 的用途是什么。只是为了验证输入吗?
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:
最佳答案
调用函数注释,请阅读此页。
https://www.python.org/dev/peps/pep-3107/
或
What does -> mean in Python function definitions?
In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.
There's no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.
用户:Katriel
TreeNode
本身是二叉树数据结构的一部分,我不知道提到的具体leetcode问题,但本质上树是由许多TreeNode
组成的> 通过变量 self.left
和 left.right
引用其他 TreeNodes
,这些名称用于模拟传统的可视化二叉树看起来像。他们只是定义节点,以便您可以了解回答问题时期望使用什么类。
关于python - 了解 : "def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:" on leetcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57732591/