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. iOS 学习 - 14.本地联系人

    苹果在iOS9的SDK中废除了AddressBookUI.framework的一些功能(是不是这个库都废除了,有待验证),具体和保存联系人相关的几个系统界面如下:联系人选择:AddressBookUI ...

  2. iOS通讯录开发

    场景一:直接选择一个联系人的电话号码 这里不需要先获取所有的联系人自己做联系人列表,直接使用系统自带的AddressBookUI/ABPeoplePickerNavigationController. ...

  3. 2.3 CMMI2级——项目跟踪和控制(Project Monitoring and Control)

    计划不是用来看的,是用来执行的.PP讲述了如何做计划,PMC讲述的就是如何跟踪计划的执行并在实际情况偏离计划时采取纠正行动. 我们先看看SG1,SG1讲述的是如何根据计划来跟踪计划的执行问题. SG1 ...

  4. SQLServer安装正常服务启动正常却无法连接

    最近给服务器安装sql2008R2版本,发现一个抓狂的问题,我自己觉得,用sql多年,从2005版本到2012版本都从安装到使用都很熟练,竟然被2008版本难住了 给服务器正常安装,sql2008r2 ...

  5. PowerBI通过gateway连接多维数据库

    简介   Microsoft Power BI 是由微软推出的商业智能的专业分析工具,给用户提供简单且丰富的数据可视化及分析功能.个人非常喜欢,有免费版和Pro的付费版,今天主要是介绍下通过gatew ...

  6. jqGrid 最常用的属性和事件,供平时参考(转)

    [html] <html> ... <table id="list1"></table> <div id="pager1&quo ...

  7. ORA-01858: 在要求输入数字处找到非数字字符

    数据库   date  字段问题  insert into  WK_RE_LE  (DACL_FILE_ID,DACL_GROUP_ID,BDCDYH,DACL_LENGTH,ISVALID,DACL ...

  8. oracle常用命令集合

    一. 表空间相关命令 创建数据表空间 create SMALLFILE tablespace dataSpace datafile 'E:\oracle\product\10.2.0\oradata\ ...

  9. 烂泥:SQL Server 2005数据库安装

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 为了能更好的利用服务器,所以打算把该业务进行迁移.因为该业务比较特殊,需要服务器上有相应的硬件支持,所以打算直接升级该服务器目前的操作系统.目前公司服务 ...

  10. Xcode同时兼容Xcode7和Xcode8,两个版本并存,也适用于先升8再安装7

    先吐槽一下之前看到的一个教程,如下: 先在应用程序内,拷贝一份之前的xcode,然后再安装新版本,发现这种安装完成就是在之前上面迭代了  有木有?等于没任何作用 我这边就是不小心先升级了8,然后再安装 ...