python - 了解 : "def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:" on leetcode

标签 python algorithm class

我正在解决 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.leftleft.right 引用其他 TreeNodes,这些名称用于模拟传统的可视化二叉树看起来像。他们只是定义节点,以便您可以了解回答问题时期望使用什么类。

https://www.geeksforgeeks.org/binary-tree-data-structure/

关于python - 了解 : "def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:" on leetcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57732591/

相关文章:

python - 线条颜色随 matplotlib 中折线图的数据索引而变化?

algorithm - 有重量和元素限制的背包

html - 容器内的CSS中心div?

algorithm - Java 中的流水线归并排序

c++ - 类成员的初始化顺序,C++

javascript - 为什么这个选择器不起作用? JS

python - 合并两个 Pandas 交叉表 : Index and Col Name Issues

python - 如何在Python中将内存中的WebM音频文件转换为mp3音频文件?

python - 无法在 heroku django 中导入名称 _uuid_generate_random

python - 为什么这个脚本的运行时间是 O(n^2)?