leetcode-easy-trees-98. Validate Binary Search Tree-NO
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的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【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) ...
- [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 ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- [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 ...
随机推荐
- 关于ES5中的prototype与ES6中class继承的比较
ES5:继承: 1.ES5:继承 通过原型链实现继承.子类的prototype为父类对象的一个实例,因此子类的原型对象包含指向父类的原型对象的指针,父类的实例属性成为子类原型属性 2.ES6 的继承 ...
- Mybatis实际练习
1.mybatis在xml文件中处理大于号小于号的方法 第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE 1 = 1 AND ...
- 使用GDB和GEF进行调试
使用GDB进行调试 这是编译ARM二进制文件和使用GDB进行基本调试的简单介绍.在您按照教程进行操作时,您可能需要按照自己的习惯使用ARM程序集.在这种情况下,你要么需要一个备用的ARM设备,或者你只 ...
- Spring中配置Hibernate事务管理
<!-- transationManager --> <bean id="transactionManager" class="org.springfr ...
- 使用NPOI或POI 导出Excel大数据(百万级以上),导致内存溢出的解决方案(NPOI,POI)
使用工具:POI(JAVA),NPOI(.Net) 致谢博主 Crazy_Jeff 提供的思路 一.问题描述: 导出任务数据量近100W甚至更多,导出的项目就会内存溢出,挂掉. 二.原因分析: 1.每 ...
- 在 Android 中实现 Redux 的一点经验
简评: Redux 是一个用于应用程序状态管理的开源JavaScript库,其核心是通过可管理和控制的状态来描述一个系统.这意味着其思想其实是可以应用于任何类型应用的开发的,包括移动应用. Redux ...
- POJ2449 K短路模板
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- 清北学堂dp图论营游记day3
.状态压缩dp: 对于这个我们引入二进制状态压缩,因为任何一个数都可以二进制表示,而其二进制表示上每一位都可以表示当前位置是否有元素,这就构成了状态压缩. 对于这个题,上下行&一下就行. 状压 ...
- Linux修改mysql配置文件
1.首先需要知道mysql数据库安装在什么位置 2.查找配置文件位置 然后在根据这个目录,查看配置文件在哪里了(路径后面加上 --verbose --help|grep -A 1 'Default o ...
- Django-多对多建表与Form表单
一.多对多建表的三种创建方式: 1.全自动型:(一般情况下使用) class Book(models.Model): title = models.CharField(max_length=32) a ...