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. 1.phpcms 伪静态

    location / { if (!-f $request_filename){ rewrite (.*) /index.php; } rewrite ^/caipu-([-]+)-([-]+)-([ ...

  2. python3学习笔记.5.打包发布

    为了给别人使用将 .py 文件打包成 .exe 文件 安装 PyInstaller  ,完成 打开 Cmd 调用 path:\pyinstaller.exe -F path:\name.py 发布文件 ...

  3. v4l2API无法执行VIDIOC_DQBUF的问题

    一.PC虚拟机下ubuntu14.04.1环境下 源文件:show.c  USB摄像头格式yuyv: 申请缓冲帧数2,分辨率640*480  阻塞无法执行 申请缓冲帧数4,分辨率640*480  阻塞 ...

  4. sqlplus设置长度

    1.set linesize   100 2.col  XX format  a30 3.col  XXX format 9,999,999,999 3.set heading off  表头不显示

  5. mysql优化【转】

    最近听讲了博森瑞老师的mysql优化公开课,这个是我整理的笔记. 现在说一下mysql的内存和I/O方面的两个特点. 一. mysql内存特点: 1.  也有全局内存和每个session的内存(每个s ...

  6. 我看到的最棒的Twisted入门教程!

    http://www.douban.com/note/232204441/ http://www.cnblogs.com/sevenyuan/archive/2010/11/18/1880681.ht ...

  7. HDU 6214 Smallest Minimum Cut 最小割,权值编码

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...

  8. mac搭建lamp环境

    转载:https://www.cnblogs.com/beautiful-code/p/7465320.html

  9. POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)

    题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...

  10. centos 6.x 部署uwsgi+flask项目

    一.项目背景 1. 公司需求要做一个在线统计页面; 2. 统计在线人数,进行人数数据展示; 3. 类似QQ官网在线人数 二.测试环境 [root@linux-node2 ~]# cat /etc/*r ...