[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) ...
随机推荐
- Equipment Box[HDU1110]
Equipment Box Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- C#中DataTable使用技巧
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...
- object-c 协议和委托
协议相当于接口 委托相当于帮助实现其它类的功能 object-c提供的协议机制,一个类可以实现多个协议,从而感觉上像多继承一样
- TYVJ P1069 cowtour 看不懂题意
描述 农民John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,农民John就有多个牧场了. Joh ...
- 【wikioi】1002 搭桥(dfs+最小生成树)
http://wikioi.com/problem/1002/ 今天开始又开始刷水了哈T_T.照着hzwer神犇的刷题记录刷!!! 题解: 一开始我也不会,但是我想到了直接爆搜T_T. 好吧,题解. ...
- 使用RBTool自动提交code review请求
使用RBTool自动提交code review请求 前言 让我们回想一下手工提交review请求的过程: 首先得用 svn diff > filename.diff 生成diff文件. 然后输入 ...
- for循环计算游戏通关分数
一个游戏1到20关是成绩是自身关卡数的成绩,21-30每关10分,31-40每关20分,41-49每关30分,50关100分.输入一个关数求成绩 代码如下: <!DOCTYPE html PUB ...
- 源码安装Postgresql9.4.1
1.先到官网下载http://www.postgresql.org/ftp/source/v9.4.1/ tar包 2.解压后执行: sudo apt-get install zlib1g-dev s ...
- easyui datagrid 列显示和隐藏
//当查询IT基础设施的时候隐藏'STAFF_ID'.'ITSM_STAFF_ID' if($("input[name='currentstate']").val()==2){ $ ...
- Shrink磁盘
30857(分区的总容量) = 10160(可修改的这个10610表示Shrink之后的空闲分区) + 20697(Shrink之后分区中占用掉的容量)