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设计模式》笔记一js的表现力

    用不同方法完成同样一个任务:启动和停止动画. 1.过程式的程序设计: function startAnimation(){ ... } function stopAnimation(){ ... } ...

  2. Codeforces Round #375 (Div. 2) F. st-Spanning Tree

    传送门 分析:构造题.可以这么想:先把s,t两个点去掉,把剩下的点先并查集合并.这样会出现个集合:, , 个剩余集合.那么个集合中先把只能与或中一个相连的连起来,如果这样已经超出了要求,那么就不能构造 ...

  3. Ecshop 后台增加一个左侧列表菜单menu菜单的方法

    Ecshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages ...

  4. x64内联汇编注意点

    #include <windows.h> #include <stdio.h> extern "C" int MyPrintf(ULONGLONG,ULON ...

  5. mysql数据库使用

    C#操作Mysql数据库的存储过程,网址 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(date1,date2) date1 和 date2 参数是合法的日期或日期/时间 ...

  6. C++11 变长模版和完美转发实例代码

    C++11 变长模版和完美转发实例代码 #include <memory>#include <iostream>#include <vector>#include ...

  7. 深入了解 JavaScript 中的 for 循环

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...

  8. 动态加载JS脚本

    建立dynamic.js文件,表示动态加载的js文件,里面的内容为: function dynamicJS() { alert("加载完毕"); } 如下方法中的html页面和dy ...

  9. Xcode中的Version和Build的区别

    Version( 应用程序发布版本号 ) Version对应的是CFBundleShortVersionString. Version 一般由产品部门确定,版本号是由分隔的整数组成的字符串,一般有2段 ...

  10. HOJ 1004: Prime Palindromes

    问题:输入两个整数 a 和 b (5 <= a < b <= 1,000,000,000),输出 [a, b] 内的所有回文质数. 最简单的暴力解法是依次遍历 [a, b] 范围内的 ...