python - 我的 python 查找函数返回 None 对象,尽管它准确地找到了密钥
问题描述
我正在编写代码以在二叉树中查找具有特定键的节点,尽管它可以正确找到节点但返回无
def Find(key,root):
if(root):
if (key>root.key):
print("going right")
Find(key,root.right)
elif (key<root.key):
print("going left")
Find(key,root.left)
elif (key == root.key):
print("found" )
print(root)
return root
else :
print("not in the tree")
return 0
temp = Find(5, bt.root) //Find the node with key 5 in the tree
print(type(temp))
// Output >>
<__main__.Node object at 0x000000F8A7DCAAC8>
<class 'NoneType>
为什么它返回 None 类型,因为函数显然返回节点
解决方案
更改Find(key,root.right)
为return Find(key,root.right)
(与左侧类似)。
目前,当你找到元素时,你并没有返回它,所以函数隐式返回None
。
推荐阅读
- python - 实时平滑 2D 点(分散)(在线平滑)
- unity3d - HoloLens 2的分辨率真的是2048*1080吗?
- reactjs - 是否有任何工具/软件可以为 ReactJs 自动生成单元测试文件?
- android - 无法在 Google Pixel 3a 上捕捉粗略位置
- android-studio - 在片段中使用回收站视图时出错
- javascript - 为什么 Redux Provider 标签会破坏整个 App
- python - PySide6 setPageMargins()
- ruby-on-rails - 无法通过 GoogleOauth2 对您进行身份验证,Devise。导轨 6
- draftjs - 将 contentState 渲染到编辑器后,Draft.js 提及插件不起作用
- google-chrome - 谷歌使用了哪些语言/资源来创建“东京涂鸦奥运会”?