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. idea 启动不了

    idea 更新了新版本.  破解步骤安排完了之后 ,  发现怎么也启动不了.  没有任何提示. 于是为了查看错误信息去idea的安装目录bin下用idea.bat脚本启动发现了如下错误: 在idea6 ...

  2. CentOS 6 搭建SVN支持httpd和svnserve独立服务器两种模式 以及邮件配置

    Linux下SVN服务器同时支持Apache的http和svnserve独立服务器两种模式且使用相同的访问权限账号 服务器操作系统:CentOS 6.x 1.在服务器上安装配置SVN服务: 2.配置S ...

  3. java中常用的集合的一些区别 (2013-10-07-163写的日志迁移

    java中的以下几大集合: List结构的集合类: ArrayListl类,LinkedList类,Vector类,stack类 Map结构的集合类: HashMap类,Hashtable类(此是以k ...

  4. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

  5. android自动化测试之Monkey--从参数讲解、脚本制作到实战技巧

    视频: http://v.youku.com/v_show/id_XODcyMjM1MDA4.html?from=y1.2-1-87.4.4-1.1-1-2-3 PPT: http://www.doc ...

  6. 轻量级的C++插件框架 - X3 C++ PluginFramework

    X3 C++ PluginFramework 代号为X3的C++轻量级通用插件框架平台是一套通用的C++轻量级插件体系,没有使用MFC.ATL.COM.可在Windows和Linux下编译运行.应用程 ...

  7. install cinnamon on ubuntu 14.04

    emotion: I feel not comfortable with ubuntu 14.04 default desktop unity,i still look for a alternati ...

  8. Ruby 符号【转】

    Ruby的符号足以让很多初学者迷惑上一段时间,看过本章节后,或许会解开你心中的疑惑. 在Ruby中,一个符号是就是一个Symbol类的实例,它的语法是在通常的变量名前加一个冒号,如 :my_sy Ru ...

  9. maven项目打包jar,含有依赖jar

    在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...

  10. Jboss性能调优

    1,Jboss5调优指南 https://www.redhat.com/f/pdf/JB_JEAP5_PerformanceTuning_wp_web.pdf 1,Jboss7.1 性能调优指南 a: ...