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. poj 2166 构造

    一个看了解题报告才能想明白的题目,第一点比较容易想明白,就是每次把1交换到堆顶之后如果能够换到最后面的位置那么一定是最优的,但是怎么实现这个没有想明白. 题解的那种构建方法,及从2开始插入,是可以保证 ...

  2. Error : APP-FND-01926: The custom event WHEN-LOGON-CHANGED raised unhandled exception: ORA-06502: PL

    In this Document   _afrLoop=440418974213449&id=1508865.1&_afrWindowMode=0&_adf.ctrl-stat ...

  3. SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中

    SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中 SOLOWHEEL俱乐部聚会活动火热报名中

  4. cocos2dx使用tolua关于字符串处理的一个问题

    正在使用cocos2dx的tolua binding在此过程中发现的一个问题.假设一回或输入是std::string当我们不同意包括二进制数据,和std::string我同意,这样一来就导致了不正确的 ...

  5. 单片机实验: 三轴磁场模块 GY-271

    最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...

  6. 多个UpdatePanel控件相互引发刷新的使用

    原文:多个UpdatePanel控件相互引发刷新的使用 ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异 步局 ...

  7. U5首次登录

    1.在Llinx中,大小写字母是不一样的东西. 2.date可以查看日期,date的正确格式是:date +%Y/%m/%d/%H/%M(左边这句话所想表达的意思是年的字母必须为大写,月的必须为小写. ...

  8. 【android】在Eclipse在联想引jar包源代码

    (前提是你有jar包源代码!!) .确保Referenced LIbraies下已经有该jar包,否则的话,右击该jar包选build path->add to build path. 二.右键 ...

  9. Unity3D发布WebPlayer时Failed to download data file解决方案

    今天发布WebPlayer时, 发现直接打开html是可以正常运行的, 但是通过iis访问的话就会报错: Failed to download data file. 一开始以为是防火墙, 后来发现不是 ...

  10. PL/SQL“ ORA-14551: 无法在查询中执行 DML 操作”解决

    环境 Oracle 11.2.0 + SQL Plus 问题 根据以下要求编写函数:将scott.emp表中工资低于平均工资的职工工资加上200,并返回修改了工资的总人数.PL/SQL中有更新的操作, ...