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

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

    2
/ \
1 3

Binary tree [2,1,3], return true.

Example 2:

    1
/ \
2 3

Binary tree [1,2,3], return false.

先中序遍历树到数组flag,flag中必定是从小到大排列,如果不是,return false

 class Solution(object):
def isValidBST(self, root):
flag = []
self.isv(root,flag)
for i in range(1,len(flag)):
if flag[i]<=flag[i-1]:
return False
return True
def isv(self,root,flag):
if root == None:
return
self.isv(root.left,flag)
flag.append(root.val)
self.isv(root.right,flag)

[leetcode tree]98. Validate Binary Search Tree的更多相关文章

  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

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

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

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

  5. 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 ...

  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 define ...

  7. 【LeetCode练习题】Validate Binary Search Tree

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

  8. [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 ...

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

随机推荐

  1. SpringBoot与异步任务、定时任务、邮件任务

    异步任务 在需要开启异步的服务加上注解:@Async @Service public class AsyncService { //告诉SpringBoot这是一个异步任务,SpringBoot会自动 ...

  2. UNIX环境高级编程 第6章 系统数据文件和信息

    UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...

  3. spring-boog-测试打桩-Mockito

    Mockito用于测试时进行打桩处理:通过它可以指定某个类的某个方法在什么情况下返回什么样的值. 例如:测试 controller时,依赖 service,这个时候就可以假设当调用 service 某 ...

  4. App劫持病毒剖析:你的应用是如何被替换的(病毒防范方法)

    App劫持病毒剖析:你的应用是如何被替换的(病毒防范方法) 一.App劫持病毒介绍 App劫持是指执行流程被重定向,又可分为Activity劫持.安装劫持.流量劫持.函数执行劫持等.本文将对近期利用A ...

  5. 42 - 数据库-orm-SQLAlchemy

    目录 1 ORM 2 sqlalchemy 3 基本使用 3.1 创建连接 3.1.1 利用连接池执行sql 3.1.2 利用session来执行sql 3.2 创建基类 3.3 创建实体类 3.3. ...

  6. SQL 存储过程分页

    CREATE PROC p_Team_GetTemaList @pageindex INT , @pagesize INT , @keywords VARCHAR(200) , --模糊查询 名称 标 ...

  7. CKEDITOR的内容js转码,C#控制器解码接收

    <script type="text/javascript" src="<%=Url.Content("~/Resource/ckeditor/ck ...

  8. 【前端开发】限制input输入保留两位小数

    <input type="text" name='amount' id="cash_num" placeholder="请输入金额" ...

  9. 【Android开发日记】之入门篇(八)——Android数据存储(下)

    废话不多说了,紧接着来讲数据库的操作吧.Come On! 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基 ...

  10. java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)

    本文知识点(目录): 1.while循环语句    2.do...while循环语句    3.for循环语句    4.for...in循环语句    5.附录1(with语句)    6.附录2( ...