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

判断一棵树是否为二叉搜索树。key 是,在左子树往下搜索的时候,要判断是不是子树的值都小于跟的值,在右子树往下搜索的时候,要判断,是不是都大于跟的值。很好的一个递归改进算法。

简洁有思想!

 #include<climits>

  // Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
bool subfunction(TreeNode *root,int min,int max)
{
if(root == NULL)
return true; return (root->val>min && root->val < max &&subfunction(root->left,min,root->val) && subfunction(root->right,root->val,max)); }
bool isValidBST(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root ==NULL)
return true; return subfunction(root,INT_MIN,INT_MAX); }
};

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

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

  3. 【leetcode】 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 题解]: 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. Java for LeetCode 098 Validate Binary Search Tree

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

  6. [LeetCode] 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 ...

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

  8. 【leetcode】Validate Binary Search Tree(middle)

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

  9. 【题解】【BST】【Leetcode】Validate Binary Search Tree

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

随机推荐

  1. Git for Windows 工具的使用(一)

    如果你还不知道什么是Git,只知道GitHub,但是还不会用,我想这个教程会帮助你. 前言 鉴于网上目前的教材都太落后,GitHub for Windows已经更新了多个版本,好多界面都发生了变化,所 ...

  2. WPF触控程序的开发(一)——有用的资源

    迟来的一篇博文,每次都要撞到月末,这个月实在太忙了,除了在公司上班,还接了个单子,用wpf做一个触屏软件,类似iphone的相册功能.先说搭建开发环境吧,我是不可能去买个平板来的,再说基于win7的程 ...

  3. No identifier specified for entity: XXXX 错误

    在运行项目的时候报了下面的错误: by: org.hibernate.AnnotationException: No identifier specified for entity: com.exam ...

  4. adb -a server nodaemon,设备一直显示 offline,而 adb devices 一直显示 device【已解决】

    1. adb -a server nodaemon 一直显示 offline 2. adb devices 一直显示 device 谷歌 和 度娘了一圈,未寻得解决办法 # 解决方法 问题已解决,使用 ...

  5. Python 前端的第三方库

    sweetalert sweeralert:地址 这个使用很简单,需要在在他们的,css和js文件. 酷炫的结果    datatables datatables:地址 已https://datata ...

  6. js 页面刷新 每N秒钟刷新一次页面

    <!-- 每5秒钟刷新一次页面 -->     <script>setTimeout("location=location; ", 5000); </ ...

  7. ccna学习指南第七版

    1.加电post自检    闪存查找ios 可随时从命令行进入设置模式,为此可在特权模式下输入setup    ctrl+c退出特权模式 6.2cli   命令行界面 进入cli router> ...

  8. how can I ues Dataset to shuffle a large whole dataset?

    The Dataset.shuffle() implementation is designed for data that could be shuffled in memory; we're co ...

  9. Django创建并连接数据库(实现增删改查)--ORM框架雏形

    第一步:要先创建数据库(orm是不能创建数据库的) 第二步:settings里面指定连接到哪个数据库 DATABASES = { #默认使用的是sqlite3数据库 'default': { 'ENG ...

  10. 2018天梯赛第一次训练题解和ac代码

    随着评讲的进行代码和题解会逐步放上来 2018天梯赛第一次训练 1001 : 进制转换 Time Limit(Common/Java):1000MS/10000MS     Memory Limit: ...