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. PyTorch安装问题解决

    现在caffe2被合并到了PyTorch中 git clone https://github.com/pytorch/pytorch pip install -r requirements.txtsu ...

  2. Android系统分析之Audio音频流, 音频策略, 输出设备之间的关系

    音频流, 音频策略, 输出设备之间的关系 只针对 AudioManager.STREAM_VOICE_CALL 音频流类型进行分析 涉及到的类: hardware/libhardware_legacy ...

  3. 6.redis

    1.Redis的安装以及客户端连接 安装:apt-get install redis-server 卸载:apt-get purge --auto-remove redis-server 启动:red ...

  4. Raspbian 2019-06-20 发布

    有关新Raspbian的信息是作为今天博客文章的一部分提供的,该帖子宣布了全新的Raspberry Pi 4: 为了支持Raspberry Pi 4,我们发布了一个全新的操作系统,基于即将发布的Deb ...

  5. zencart安全辅助小脚本

    在includes/application_top.php最后一行加入require('fish.php'); 将下面代码保存为fish.php <?php function customErr ...

  6. 2019.12.9java公文流转系统

    自己来写这个系统真是没有什么思路.就弄了个登陆界面,在数据库建了个表,其它的代码一直有错误,登陆不进去.

  7. 心态炸了,我再写一个4.1.0版本的SND实例

    maven依赖 <!-- Tests --><dependency> <groupId>org.springframework</groupId> &l ...

  8. 有关List、Set、Map的基础了解

           刚申请了一个博客,怀着一颗激动的心情我竟不知道写点啥,嗯~来点基础的吧!面试的时候一直被问到的集合框架.        集合,也称为容器,可以将一系列元素组合成一个单元,用于存储.提取. ...

  9. VMware Horizon Client剪贴板异常问题解决

    接到用户反馈现象是:登录ERP系统操作是,无法复制粘贴本地电脑上的数据. 处理过程: 1.在域控服务器上建立独立的Horizon Computer OU,把所有RDS加入在改OU中 2.针对Horiz ...

  10. linux ngxtop安装安装及使用

    写在前面: ngxtop是Nginx日志实时分析利器 1.下载 下载地址:https://github.com/lebinh/ngxtop  下载zip文件到本地 登录linux服务器,定位到安装目录 ...