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. FTP服务器上传,下载文件

    public class FtpUtil { /** * * @param host FTP服务器地址 * @param port FTP服务器端口 * @param username FTP登录账号 ...

  2. 51. N-Queens (JAVA)

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  3. 12、Nginx代理缓存服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  4. linux 网卡配置详情

    1.配置文件/etc/hosts(本地主机ip地址映射,可以有多个别名)./etc/services(端口号与标准服务之间的对应关系)./etc/sysconfig/network(设置主机名,网关, ...

  5. shell 实用脚本2

    脚本功能 拷贝文件夹 及 子文件夹内文件  到 对应的 文件结构下 ,且拷贝前先进行备份 #!/bin/sh #create by lizr -- #脚本功能 #覆盖文件前先备份 cfsuffix=$ ...

  6. 未能加载文件或程序集“System.Web.Http.WebHost

    http://blog.csdn.net/yuanzhugen/article/details/46625353(转)

  7. 执行dlsym()函数出现: undefined symbol

    执行dlsym()函数出现: undefined symbol 执行dlsym()函数出现: undefined symbol 当这个问题出现的时候,可以检查产生so文件的cpp文件,看看是否已经用  ...

  8. button 文字图片上下/左右经常会用到,记录一下

    上下:    self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平 ...

  9. 2019全国卷(III)理科23题的另类解法

    已知 $x,y,z\in\textbf{R}$且$x+y+z=1$ (1)求$(x-1)^2+(y+1)^2+(z+1)^2$的最小值: (2)若$(x-2)^2+(y-1)^2+(z-a)^2\ge ...

  10. CH5101 LICS//hdu5904 LICS

    恭喜我已经正式沦为pj组选手QwQ 标题两个题其实不一样的.这是ch   这是hdu 一.CH上的:裸题,求LICS.n<=3000 经典普及组dp题,题解烂大街了.所以对于这题,只讲细节: $ ...