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.
回想一下BST的定义,任一节点的子孙分别递归满足。左子孙小于该节点,右子孙大于该节点。仅仅要推断这个就OK了;
一个主意点:在递归程序里面。仅推断一个节点大于左儿子小于右儿子是不够的,这样对于左儿子的右儿子以及右儿子的左儿子的
错误推断不到,因此须要将节点值变为边界值,递归下传;
代码例如以下:
class Solution {
public:
bool isValidBST(TreeNode *root) {
return check(root, INT_MIN, INT_MAX);
} private:
bool check(TreeNode *root, int left, int right){
if(root == NULL)
return true;
return (root->val > left) && (root->val < right)
&& check(root->left, left, root->val) &&check(root->right, root->val, right);
}//这里的左儿子的左界用上面传下来的,右界用节点值,右儿子镜面对称
};

PS:依照注意点提到的思路写的错误代码

/**
* 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 isValidBST(TreeNode *root) {
if (root == NULL)
return true; bool bleft = true, bright = true;
if (root->left != NULL){
if (root->val > root->left->val){ //注意这里检验以及递归是不能保证根节点大于左边全部的。矛盾在于,左儿子的右儿子,以及右儿子的左儿子。
bleft = isValidBST(root->left);
}
else{
return false;
}
} if (root->right != NULL){
if (root->val < root->right->val){
bright = isValidBST(root->right);
}
else{
return false;
}
}
return bleft && bright;
}
};


版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode :: 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] Validate Binary Search Tree 验证二叉搜索树

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

  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. LeetCode: Validate Binary Search Tree [098]

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  6. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

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

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

随机推荐

  1. ON、WHERE、HAVING的差别

    ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.以下我们来分析三者之间的差别. 1.       ON 和WHERE 全部的查询都回产生一个中间暂时报表,查询结果就是从 ...

  2. hdu1876(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...

  3. hdu2639(背包求第k优解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 分析:dp[i][j][k]表 ...

  4. Struts2 学习第一步准备工作

    第一步:安装下载MyEclispe10 对于MyEclispe的下载安装就不再详述了. 第二步:下载Struts-2.3.15 Struts-2.3.15下载地址: http://struts.apa ...

  5. IOC框架之一Autofac

    .NET领域最为流行的IOC框架之一Autofac 一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程序也 ...

  6. 【译】ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据

    原文:[译]ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据 在本节中,你将新建一个MoviesController 类,并编写获取电影数据的代码,使用视图模板将数据展示在浏览器中. ...

  7. java中浮点数的比较(double, float)(转)

    问题的提出:如果我们编译运行下面这个程序会看到什么? public static void main(String args[]){ System.out.println(0.05+0.01); Sy ...

  8. 《火球——UML大战需求分析》(第1章 大话UML)——1.3 行为型的UML(Behavior Diagram)

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  9. 【转】Acm之java速成

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入:格式为:Scanner cin = new Sca ...

  10. e​c​s​h​o​p​模​板​ l​b​i​文​件

    Ecshop根目录/ |->其它目录|->themes |->例:default (模板项目目录) |->images                             ...