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. vue iview面包屑

    简单看一下vue,iview的面包屑怎么写呢? 简单的思路:1.获取到路由记录$route.matched 2.渲染 效果: 一.$route.matched 官网地址:https://router. ...

  2. mysql的导入导出操作

    mysqldump工具基本用法 此方法不适用于大数据备份 备份所有数据库 mysqldump -u root -p --all-databases > all_database_sql 备份my ...

  3. Apache(web服务器)与Tomcat(应用服务器)搭建集群

    web服务器:Apache.Nginx.IIS等 应用服务器:Tomcat.JBoss.Weblogic等 现在web服务器和应用服务器其实界限已经不是太清晰了,大部分的应用服务器也包含一些web服务 ...

  4. python、第四篇:记录相关操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  5. Linux驱动开发之字符设备驱动模型之file_operations

    90%的驱动模型都是按照下图开发的 下面来说下设备描述结构是什么东西 打开Linux-2.6.32.2的Source Insight 工程,搜索cdev 比如一个应用程序需要调用read和write这 ...

  6. glsl:error C1105: cannot call a non-function

    今天写的shader编译过程中报了这个错误,而且错误行数是0.原因怎么找也找不到.后来发现原来是normalize方法写成了了normal正好和函数的形参名字一样. 特地记录一下.

  7. The Preliminary Contest for ICPC Asia Nanchang 2019 B. Fire-Fighting Hero

    题目:https://nanti.jisuanke.com/t/41349 思路:dijkstra最短路径 先以 fire-fighting hero为起点 跑一遍dijkstra 建立 起点 p 并 ...

  8. Spring中 aop的 xml配置(简单示例)

    示例: aop,即面向切面编程,面向切面编程的目标就是分离关注点. 比如:小明(一位孩子)想吃苹果,首先得要有苹果,其次才能吃.那么妈妈负责去买水果,孩子负责吃,这样,既分离了关注点,也减低了代码的复 ...

  9. Android APK 手动签名

    首先,如果没有签名密钥,先生成密钥: keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore and ...

  10. jquery $(".classc",$(".classp"))的含义

    jquery中有一种选择器的写法为:$(".classc",$(".classp")),注意,是$()中又嵌套了一个$(),这种写法的作用类似于$(" ...