Validate Binary Search Tree

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.

分析: 判断中序遍历的是不是逐渐增大的,如果不是,return false, 如果是,return true。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root==NULL)
return true;
stack<TreeNode*> s;
TreeNode* cur=root;
TreeNode* pre=nullptr;
while(){ while(cur){
s.push(cur);
cur =cur->left;
}
if(s.empty())
break;
cur = s.top();
s.pop();
if(pre!=nullptr && pre->val >= cur->val)
return false;
pre = cur;
cur = cur->right;
} return true;
}
};

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】Validate Binary Search Tree

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

  3. LintCode Validate Binary Search Tree

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

  4. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  5. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

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

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

  7. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  8. LeetCode: Validate Binary Search Tree 解题报告

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

  9. 【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) ...

随机推荐

  1. Android touch mode和focusableInTouchMode分析

    首先我们来看看touch mode的定义.它是用户和手机进行交互时view层次结构的一个状态.它本身是很容易理解的, 代表了最近一次的交互是否是通过触摸屏发生的,因为在Android设备上还存在别的交 ...

  2. iOS常见三方总结(更新中)

    常用的三方积累 MJExtention SSZipArchive 用于解压与压缩文件 地址:https://github.com/ZipArchive/ZipArchive ps:比较奇葩,githu ...

  3. VS2015 Git 插件使用教程

    VS2015 中继承了 Git 插件,再也不用下载 Github for Windows了. 从 团队-管理连接  中打开 团队资源管理器 克隆Repository 在 本地 Git 存储库下面点击 ...

  4. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

    struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

  5. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  6. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  7. Spring AOP 动态代理 缓存

    Spring AOP应用:xml配置及注解实现. 动态代理:jdk.cglib.javassist 缓存应用:高速缓存提供程序ehcache,页面缓存,session缓存 项目地址:https://g ...

  8. IO系统性能之一:衡量性能的几个指标

    作为一个数据库管理员,关注系统的性能是日常最重要的工作之一,而在所关注的各方面的性能只能IO性能却是最令人头痛的一块,面对着各种生涩的参数和令人眼花缭乱的新奇的术语,再加上存储厂商的忽悠,总是让我们有 ...

  9. 烂泥:puppet3.7安装与配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 有关服务器的自动化管理,这方面以前没有接触过.打算这段时间把这块知识给补上. 现在服务器自动化管理软件,使用最多也最火的就是puppet了. 那么我们今 ...

  10. linux学习之路——ubuntu 16.04 开机开启数字小键盘解决方法

    第一步:安装numlockx,输入命令 sudo apt-get install numlockx 第二步:用 vim 打开 rc.local 文件,输入命令 sudo vim /etc/rc.loc ...