节点是否出现在二叉树中?python如何在给定代码中返回True或false
def isNodePresent(root, x):
if root is None:
return
if root.data == x:
return True
isNodePresent(root.left, x)
isNodePresent(root.right, x)
发布于 2020-04-15 00:54:49
您可以使用一个包装器(如果您不能更改代码)来完成返回true或false的工作
def isNodePresent(root, x):
if root is None:
return
if root.data == x:
return root.data
isNodePresent(root.left, x)
isNodePresent(root.right, x)
def returnBooleanIfNodePresent(root, x):
if isNodePresent(root, x) == None:
return false
else:
return true
希望这能有所帮助。
https://stackoverflow.com/questions/61219548
复制