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.

 /**
* 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) {
return isValidBST(root, LONG_MIN, LONG_MAX);
}
bool isValidBST(TreeNode *root, long mn, long mx) {
if (!root) return true;
if (root->val <= mn || root->val >= mx) return false;
return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
}
};

Leecode_98_Validate_Binary_Search_Tree的更多相关文章

随机推荐

  1. [Luogu5384][Cnoi2019] 雪松果树

    传送门 虽然这题是一道二合一,也不算难,但还是学到了很多东西啊,\(k\) 级儿子个数的五种求法!!我还是觉得四种比较好( \(k\) 级儿子个数有五种求法,你知道么? --鲁迅 首先 \(k\) 级 ...

  2. JSP——底层原理

    都知道jsp就是在HTML文件中写java代码,以实现动态页面的效果,但是这种动态是如何实现的呢?今天就在研究一下. 首先,我写了一个简单的jsp文件: <%@page import=" ...

  3. [网络]HTTP

    HTTP HTTP 简介 HTTP 协议是 Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web)服务器传输超文本到本 ...

  4. Kubernetes 挂载文件到pod里面

    下面以chart为例子: 1.创建ConfigMap,这里要注意config.js为挂载的文件名 [root@cn-hongkong templates]# cat app-config.yaml a ...

  5. pandas处理excel的常用方法技巧(上)

    1. 导库 import pandas as pd 2. 读取excel文件 这里要注意的就是第二个参数header如果不设置,pandas会默认把excel的第一行当作columns,header= ...

  6. CSS如何修改tr边框属性

    有很多时候,我们都要自定义为表格合并边框,这个只要 table{ border-collapse:collapse; } 就可以了 参数: separate 默认值.边框会被分开.不会忽略border ...

  7. Graphviz 画图的一些总结

    Graphviz Graphviz 是一个自动排版的作图软件,可以生成 png pdf 等格式. 一切以官方文档为准,博客只是参考.这里做一个自己学习的记录. dot 语法介绍 部分图形属性介绍 示例 ...

  8. 微信小程序(mpvue) wx.openSetting 无法调起设置页面

    在开发过程有个需要保存图片/视频到设备相册的业务,so easy~   巴啦啦撸下来了完整功能, wx.saveVideoToPhotosAlbum 会自动调起用户授权,美滋滋~~   btu.... ...

  9. PowerMock学习(四)之Mock static的使用

    我们编写代码的时候,总会写一些工具类,为了方便调用喜欢使用static关键字来修饰对应方法. 那么现在举例说明,还是准备两个接口,第一个是查询学生总数,第二个是新增学生两个接口,具体示例代码如下: p ...

  10. windows版本 MongoDB副本集搭建及开启身份验证

    ------------恢复内容开始------------ ------------恢复内容开始------------ MongoDB副本集搭建 我搭建的是一个主节点,两个副节点 构建目录结构如下 ...