[LeetCode]题解(python):098 Validate Binary Search Tree
题目来源
https://leetcode.com/problems/validate-binary-search-tree/
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
题意分析
Input:二叉树
Output:boolean值
Conditions:检验一个二叉树是不是有效的二叉搜索树
题目思路
注意到,每层的最大最小约束都是不一样的,所以采用dfs的思想,不断更新最大最小值。在python中,数字可以取很大,要设大一点。
AC代码(Python)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
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):
"""
:type root: TreeNode
:rtype: bool
"""
return self.validBST(root, -21474836480, 21474836470)
[LeetCode]题解(python):098 Validate Binary Search Tree的更多相关文章
- Java for LeetCode 098 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 ...
- LeetCode(98) Validate Binary Search Tree
题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- 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) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- TYVJ P1077 有理逼近 Label:坑,tle的好帮手 不懂
描述 对于一个素数P,我们可以用一系列有理分数(分子.分母都是不大于N的自然数)来逼近sqrt(p),例如P=2,N=5的时候:1/1<5/4<4/3<sqrt(2)<3/2& ...
- BZOJ3236: [Ahoi2013]作业
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
- Maya Shortcuts 常用快捷键
快捷键 功能解释 工具操作 enter 完成当前操作 ~ 终止当前操作 insert 插入工具编辑模式 w 移动工具 e 旋转工具 r 缩放工具 y 非固定排布工具 shift+Q 选择工具,(切换到 ...
- MUI - Scroll插件的使用
http://dev.dcloud.net.cn/mui/ui/#scroll 神坑1:如果在vuejs中使用,那么需要配合mui.ready(function(){}) 才能找到dom对象,具体de ...
- Careercup | Chapter 7
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- MySQL数据库管理常用命令
参考: http://blog.linuxeye.com/419.html 安装 利用RPM包安装MySQL 设置TCP 3306端口的iptables root密码管理 设置root用户 ...
- 在HTML中如何把块的边框做成圆角
adius,就是半径的意思.用这个属性可以很容易做出圆角效果,当然,也可以做出圆形效果.原理很简单,“正方形的内切圆的半径等于正方形边长的一半”.下面就做一个红色的圆. 工具/原料 Adobe Dre ...
- Tesla-> Fermi (550Ti) -> Kepler(680) -> Maxwell (750Ti) -> Volta(was Pascal)
Pascal GPU Pascal (from French mathematician Blaise Pascal) is Maxwell successor. In this news, we l ...
- The P4 Language Specification v1.0.2 Header and Fields
前言 本文参考P4.org网站给出的<The P4 Language Specification v1.0.2>的第二部分首部及字段,仅供学习:). 欢迎交流! Header and Fi ...
- 【翻译】CEDEC2012 SQUARE ENIX GPGPU实现高速GI烘培工具的方法
虽然实时GI技术已经趋于成熟了,但出于对不同平台的性能和质量的考虑, 更倾向搭配一些预计算的渲染技术来实现,如给静态物体提供GI的LightMap, 给动态物体提供GI的Irradiance Vol ...