Python 检查树是否为二进制搜索树(BST)

Python 检查树是否为二进制搜索树(BST),python,python-3.x,tree,binary-search-tree,Python,Python 3.x,Tree,Binary Search Tree,我试图解决一个二叉搜索树问题,但我不能通过所有的测试用例。如果树是二叉搜索树,我需要返回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

我试图解决一个二叉搜索树问题,但我不能通过所有的测试用例。如果树是二叉搜索树,我需要返回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)
“”
类节点:
定义初始化(自身,数据):
self.data=数据
self.left=无
self.right=无
'''
def checkBST(根目录):
如果root.left==无且root.right==无:
返回真值
如果根=无:
返回真值
如果root.left!=无:
如果root.left.dataroot.data:
返回真值
其他:
返回错误
返回chckBST(root.left)、chckBST(root)和chckBST(root.right)

如果代码中存在大量冗余的
。您可以将其简化为:

def checkBST(root):
    if root == None or (root.left == None and root.right == None):
        return True

    elif root.right == None:
        return root.left.data < root.data and checkBST(root.left)

    elif root.left == None:
        return root.right.data >= root.data and checkBST(root.right)

    return checkBST(root.left) and checkBST(root.right)
def checkBST(根目录):
如果root==None或(root.left==None和root.right==None):
返回真值
elif root.right==无:
返回root.left.data=root.data和checkBST(root.right)
返回checkBST(root.left)和checkBST(root.right)
首先,检查所有
None
条件。python中的短路保证,如果第一个条件为
False
,则不会计算第二个条件。这允许您编写简洁的语句,例如
返回root.left.data


最后,如果左侧和右侧节点都不是
None
,请不要再次调用
checkBST(root)
。这将导致无限递归

所以你没有通过一些测试的原因是因为你只检查了一个级别。例如,如果有一个
,它有一个
根.left.right.data
根.data
,那么您的代码将无法捕捉到它。这是一个很好的解释

但要点是:

  • 您的代码将通过此测试

  • 但是它不会传递这个注意
    2
    root.data

我认为这个解决方案解决了这个问题(很抱歉用JS代码回答了一个Python问题,但我相信你会明白的):

函数检查BST(根){
让isBST=true;
设BSTUtil=r=>{
让我们向左,向右
if(r.left)//在左侧向下
左=BSTUtil(右左)
if(r.right)//在右侧向下
右=BSTUtil(右)
if(left>r.data)//与父级比较
isBST=false
if(右
另外,请不要使用此代码,它使用
O(n)
空间来解决问题,我相信如果我花一些时间在这个问题上,我可以找到一个更有效的解决方案。

为什么再次调用
chckBST(root)
,应该是通过测试用例本身的第一次调用,对吗?我认为递归调用应该只针对左和右
function checkBST(root) {
    let isBST = true;
    let BSTUtil = r => {
        let left, right
        if(r.left) // Bottom out on the left side
            left = BSTUtil(r.left)
        if(r.right) // Bottom out on the right side
            right = BSTUtil(r.right)

        if(left > r.data) // Compare with parent
            isBST = false
        if(right < r.data) // Compare with parent
            isBST = false

        // Return MAX from branch
        if(!left && !right)
            return r.data
        else if(!left)
            return Math.max(right, r.data)
        else
            return Math.max(left, right, r.data)
    }
    BSTUtil(root)
    return isBST;
}