Problem Link:

https://oj.leetcode.com/problems/validate-binary-search-tree/

We inorder-traverse the tree, and for each node we check if current_node.val > prev_node.val. The code is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def isValidBST(self, root):
stack = []
prev_node = None
p = root
while stack or p:
if p:
stack.append(p)
p = p.left
else:
p = stack.pop()
if prev_node:
if prev_node.val >= p.val:
return False
prev_node = p
p = p.right
return True

【LeetCode OJ】Validate Binary Search Tree的更多相关文章

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

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

  2. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

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

  4. LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)

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

  5. 【LeetCode 99】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. 【LeetCode练习题】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  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

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

随机推荐

  1. Javascript学习笔记2.1 Javascript与DOM简介

    DOM(文档对象模型)简介 DOM(文档对象模型)针对HTML和XML文档的一个API. DOM可以将任何HTML或XML文档描绘成由多层节点构成的树形结构,它是中立于平台和语言的接口,允许程序和脚本 ...

  2. 解决POST数据时因启用Csrf出现的400错误

    第一种解决办法是关闭Csrf public function init(){ $this->enableCsrfValidation = false; } 第二种解决办法是在form表单中加入隐 ...

  3. Eclipse JUnit 生成报告

    http://blog.sina.com.cn/s/blog_8af106960102v6qh.html 对Eclipse的工程写单元测试: 第一步: 1. 一个工程有多个测试类,将测试类放到一个测试 ...

  4. 关于ssh上传文件

    今天用ssh传项目到公司总部的服务器上,报了错误: encountered 1 errors during the transfer 重启ssh再次上传还是一样的错误,然后我让公司那里重启一下服务器, ...

  5. Ubuntu 修改hosts

    Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...

  6. sh back mongo

    !/bin/shBACK_DB=ALLOUT_DIR=/home/jianyeruan/app/mongo #临时备份目录TAR_DIR=/home/jianyeruan/app/mongotar # ...

  7. SQLserver 备份和还原 失败

    错误一: 备份对于服务器“xxxxxx”失败. System.Data.SqlClient.SqlError: 无法使用备份文件 'C:\Program Files\Microsoft SQL Ser ...

  8. HTML元素隐藏和显示

    在web前端开发过程中,经常会用到隐藏和显示元素的方法 总结:1.通过JS或Jquery控制          2.通过CSS样式控制 一.Js或jquery (jquery为例) 1.隐藏元素 使用 ...

  9. run VLC in root

    sed -i 's/geteuid/getppid/' /usr/bin/vlc

  10. Dojo的Gridx使用jsonrest需要注意的地方

    在使用gridx时,如果要使用jsonrest,主要的工作主要是在服务端,服务端在返回数据时,必须在返回头里添加Content-Range: 0-9/73 属性和值,其中0表示从第0条记录开始,9表示 ...