LeetCode :: Validate Binary Search Tree[具体分析]
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.
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[具体分析]的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (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 ...
- [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 ...
- 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 ...
- 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 ...
- [leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- poj 2166 构造
一个看了解题报告才能想明白的题目,第一点比较容易想明白,就是每次把1交换到堆顶之后如果能够换到最后面的位置那么一定是最优的,但是怎么实现这个没有想明白. 题解的那种构建方法,及从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 ...
- SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中
SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中 SOLOWHEEL俱乐部聚会活动火热报名中
- cocos2dx使用tolua关于字符串处理的一个问题
正在使用cocos2dx的tolua binding在此过程中发现的一个问题.假设一回或输入是std::string当我们不同意包括二进制数据,和std::string我同意,这样一来就导致了不正确的 ...
- 单片机实验: 三轴磁场模块 GY-271
最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...
- 多个UpdatePanel控件相互引发刷新的使用
原文:多个UpdatePanel控件相互引发刷新的使用 ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异 步局 ...
- U5首次登录
1.在Llinx中,大小写字母是不一样的东西. 2.date可以查看日期,date的正确格式是:date +%Y/%m/%d/%H/%M(左边这句话所想表达的意思是年的字母必须为大写,月的必须为小写. ...
- 【android】在Eclipse在联想引jar包源代码
(前提是你有jar包源代码!!) .确保Referenced LIbraies下已经有该jar包,否则的话,右击该jar包选build path->add to build path. 二.右键 ...
- Unity3D发布WebPlayer时Failed to download data file解决方案
今天发布WebPlayer时, 发现直接打开html是可以正常运行的, 但是通过iis访问的话就会报错: Failed to download data file. 一开始以为是防火墙, 后来发现不是 ...
- PL/SQL“ ORA-14551: 无法在查询中执行 DML 操作”解决
环境 Oracle 11.2.0 + SQL Plus 问题 根据以下要求编写函数:将scott.emp表中工资低于平均工资的职工工资加上200,并返回修改了工资的总人数.PL/SQL中有更新的操作, ...