【发布时间】:2017-07-03 12:21:43
【问题描述】:
我正在尝试解决二叉搜索树问题,但我无法通过所有测试用例。如果树是二叉搜索树,我需要返回 true,否则,我需要返回 false。谁能告诉我我做错了什么?
'''
class node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
'''
def checkBST(root):
if root.left == None and root.right == None:
return True
if root == None:
return True
if root.left != None:
if root.left.data < root.data:
return True
else:
return False
if root.right != None:
if root.right.data > root.data:
return True
else:
return False
return chckBST(root.left) and chckBST(root) and chckBST(root.right)
【问题讨论】:
-
你为什么要再次调用
chckBST(root),应该是测试用例本身的第一次调用吧?我认为递归调用应该只用于左右
标签: python python-3.x tree binary-search-tree