[leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/
题意:检测一颗二叉树是否是二叉查找树。
解题思路:看到二叉树我们首先想到需要进行递归来解决问题。这道题递归的比较巧妙。让我们来看下面一棵树:
4
/ \
2 6
/ \ / \
1 3 5 7
对于这棵树而言,怎样进行递归呢?root.left这棵树的所有节点值都小于root,root.right这棵树的所有节点值都大于root。然后依次递归下去就可以了。例如:如果这棵树是二叉查找树,那么左子树的节点值一定处于(负无穷,4)这个范围内,右子树的节点值一定处于(4,正无穷)这个范围内。思路到这一步,程序就不难写了。
代码:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def ValidBST(self, root, min, max):
if root == None:
return True
if root.val <= min or root.val >= max:
return False
return self.ValidBST(root.left, min, root.val) and self.ValidBST(root.right, root.val, max) def isValidBST(self, root):
return self.ValidBST(root, -2147483648, 2147483647)
[leetcode]Validate Binary Search Tree @ Python的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- [LeetCode] Validate Binary Search Tree (两种解法)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- LeetCode :: Validate Binary Search Tree[具体分析]
Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- openQPA[01]初次认识与使用
开源项目QPA 1.项目主页:[http://protocol.sinaapp.com/] 2.项目介绍: 3.运行项目: (1)安装python2.7,并安装PyQt4. 下载地址[https: ...
- Codeforces.997C.Sky Full of Stars(容斥 计数)
题目链接 那场完整的Div2(Div1 ABC)在这儿.. \(Description\) 给定\(n(n\leq 10^6)\),用三种颜色染有\(n\times n\)个格子的矩形,求至少有一行或 ...
- 20172319 2018.10.12《Java程序设计教程》第6周课堂实践(补写博客)
20172319 2018.10.12 <Java程序设计教程>第6周课堂测验 课程:<程序设计与数据结构> 班级:1723 学生:唐才铭 学号:20172319 指导老师:王 ...
- maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet
问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: prope ...
- STM32F4 External interrupts
STM32F4 External interrupts Each STM32F4 device has 23 external interrupt or event sources. They are ...
- ElasticSearch入门 :Windows下安装ElasticSearch
这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- CSDN博客的积分计算方法和博客排名规律
开通博客一段时间了,近期莫名其妙得获得"持之以恒"的勋章,看着日益增长的积分,既兴奋又好奇.本人对CSDN博客积分的计算方法非常疑惑,也不知当中怎么回事,好奇度娘一番,并结合CSD ...
- Burp Suite扫描器漏洞扫描功能介绍及简单教程
pageuo 2017-07-25 共852828人围观 ,发现 15 个不明物体 工具新手科普 * 本文作者:pageuo,本文属FreeBuf原创奖励计划,未经许可禁止转载 众所周知,burpsu ...
- Oracle JDBC连接服务名、SID和tnsnames.ora配置的多种方式
昨天,领导安排去新服务器上部署项目,给了我数据库地址,服务名称,端口,用户名和密码.结果数据库一直连接不上,日志中的错误提示是监听未找到SID,我才明白原来我jdbc.properties中需要的是S ...
- yii开发第一部分之执行流程
一 目录文件 |-framework 框架核心库 |--base 底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集,将提供特定功能给整个应用程序),CC ...