LeetCode98-验证二叉搜索树

上个星期和导师去了华农一趟

名义上是和导师去参加一个国家级的项目

其实没我啥事

都是我导师在那口若悬河

当时和那边的本科生去了另一间会议室交流了关于GAN的知识

偶然听说大家都在用pytorch

好像最新版的也挺好用的

反正就是学术界目前主要用这个框架

工业界主要用Tensorflow(没办法,Google出品)

这两天也拿来瞧了瞧

好像也确实可以的!!!


98-验证二叉搜索树

给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:
    2
   / \
  1   3
输出: true

示例 2:

输入:
    5
   / \
  1   4
     / \
    3   6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
     根节点的值为 5 ,但是其右子节点值为 4 。

思路:

这一题我刚开始做的时候,有些天真!就想着只需用层次遍历,遍历到每一层的时候,只要当前节点的左子树上节点的值小于当前节点的值,右子树上节点的值大于当前节点的值即可,不满足此条件则不是二叉搜索树,大家可以先看看代码:

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        queue = [root]
        while queue:
            res = []
            nextQueue = []
            for point in queue:
                if point and point.left:
                    if point.left.val < point.val:
                        nextQueue.append(point.left)
                    else:
                        return False
                if point and point.right:
                    if point.right.val > point.val:
                        nextQueue.append(point.right)
                    else:
                        return False
            return True

    def createBTree(self, data, index=0):
        if len(data) == 0:
            return
        pNode = None
        if index < len(data):
            pNode = TreeNode(data[index])
            pNode.left = self.createBTree(data, 2*index+1)
            pNode.right = self.createBTree(data, 2*index+2)
        return pNode


if __name__ == "__main__":
    root_list = [5, 1, 4, None, None, 3, 6]
    root = Solution().createBTree(root_list)
    output = Solution().isValidBST(root)
    print(output)

我这儿出的问题是:没吃透二叉搜索树的判断条件:

  1. 当前节点的左子树上节点的值小于其所有祖先节点的值,而不是只有其父节点的值

2. 当前节点的右子树上节点的值大于其所有祖先节点的值,,而不是只有其父节点的值

比如这个测试用例:[10, 5, 15, null, null, 6, 20]

这种方法行不通,当时灵光一闪,其实是碰巧看到了网上一篇帖子的思路啊哈哈哈哈哈哈哈。就是中序遍历一二叉搜索树所得到的节点的值一定是单增序列的。太妙了吧,这个想到了,代码也很好写了。

代码如下:

class Solution(object):
    # 此题难点:当前节点的左子树节点上的值必须恒小于其所有父节点的值
    # 另外,有重复元素,记得去重;其实,只要有重复元素,说明一定不是二叉搜索树
    # 关键点:中序遍历得到的值是单增的
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        root_list = []

        # 中序遍历
        def traverse(root):
            if root is None:
                return
            traverse(root.left)
            if root.val is not None:
                root_list.append(root.val)
            traverse(root.right)             
        traverse(root)
        root_copy = root_list[:]
        root_list.sort()
        return True if root_list==root_copy and len(set(root_list))==len(root_list) else False
            

    def createBTree(self, data, index=0):
        if len(data) == 0:
            return
        pNode = None
        if index < len(data):
            pNode = TreeNode(data[index])
            pNode.left = self.createBTree(data, 2*index+1)
            pNode.right = self.createBTree(data, 2*index+2)
        return pNode


if __name__ == "__main__":
    root_list = [5, 1, 4, None, None, 3, 6]
    root = Solution().createBTree(root_list)
    output = Solution().isValidBST(root)
    print(output)

执行效率也算不错,在中等60%左右。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习的学习者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值