mycode   不会

注意:root的值要比左子树上所有的数大

参考

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None # in valid BST, in-order taversal would go
# from node with the smallest value (left most leaf)
# up to the node with the biggest value (right most leaf) class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True self.inorder = [] def _dfs(node): if node.left:
_dfs(node.left)
self.inorder.append(node)
if node.right:
_dfs(node.right) _dfs(root) prev = self.inorder[0].val
for node in self.inorder[1:]:
print(node.val)
if node.val > prev:
prev = node.val
else:
return False
return True
# 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 isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
""" def inorder(root, output):
if not root: return
if root.left: inorder(root.left, output)
output += [root.val]
if root.right: inorder(root.right, output)
output=[]
inorder(root, output)
for i in range(1,len(output)):
if output[i]<=output[i-1]:
return False
return True
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
import sys min_val = -sys.maxsize
max_val = sys.maxsize return self.isValidBST_Util(root, min_val, max_val) def isValidBST_Util(self, root, min_val, max_val):
if not root: return True if root.val > min_val and \
root.val < max_val and \
self.isValidBST_Util(root.left, min_val, root.val) and \
self.isValidBST_Util(root.right, root.val, max_val):
return True
else:
return False

leetcode-easy-trees-98. Validate Binary Search Tree-NO的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  2. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. [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 as ...

  4. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  6. 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 as ...

  7. leetcode 98 Validate Binary Search Tree ----- java

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. LeetCode OJ 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【LeetCode】98. Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  10. [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 as ...

随机推荐

  1. CSS行高——line-height 垂直居中等问题

    CSS行高——line-height   初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了,随着以后工作问题层出不穷,才逐渐了解到CSS并不是几个sty ...

  2. NDK: ant 错误 [javah] Exception in thread "main" java.lang.NullPointerException 多种解决办法

    1.错误提示内容 2.ant脚本对应的内容 <?xml version="1.0" encoding="UTF-8"?> <!-- ===== ...

  3. orcle_day02

    第三章:单值函数 函数分为: 1.单值函数 1.字符函数 2.日期函数 3.转换函数 4.数字函数 2.分组函数(后面的章节再做学习) 哑表dual dual是一个虚拟表,用来构成select的语法规 ...

  4. php多个数组组合算法 火车头免登录发布接口代码备忘

    火车头发布产品的时候,有颜色.尺码.性别等等产品属性,需要进行不重复的组合,变成不重复的数组 <?php function comb($a){ $a = array_filter($a); $o ...

  5. java序列化和反序列化及序列化方式

    平时我们在Java内存中的对象,是无 法进行IO操作或者网络通信的,因为在进行IO操作或者网络通信的时候,人家根本不知道内存中的对象是个什么东西,因此必须将对象以某种方式表示出来,即 存储对象中的状态 ...

  6. mvn高级构建

    指定pom文件,打包指定的module,并且自动打包这个模块所依赖的其他模块. mvn clean install -f vmc-business-parent/pom.xml -pl vmc-sch ...

  7. 【hiho1035】自驾旅行III

    题目大意:给定一棵 N 个节点的有根树,1 号节点为根节点,树边有两个权值,分别为走路的代价和开车的代价.有一个旅行者开车要从根节点出发,必须遍历给定点集,可以在任何位置停止旅行,有车时可以选择开车或 ...

  8. DevExpress ASP.NET Bootstrap v19.1版本亮点:Scheduler控件

    行业领先的.NET界面控件DevExpress 正式发布了v19.1版本,本文将以系列文章的方式为大家介绍DevExpress ASP.NET Bootstrap Controls中Rich Text ...

  9. MyEclipse使用教程:添加和更新插件(一)

    [MyEclipse CI 2019.4.0安装包下载] 通过Eclipse Marketplace目录或各种更新站点类型添加插件来自定义您的Genuitec IDE. Genuitec提供以下IDE ...

  10. Http的通信机制?

    HTTP协议即超文本传送协议(Hypertext Transfer Protocol  ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用. HTTP连接 ...