[LeetCode_98]Validate Binary Search Tree
题目链接
https://leetcode.com/problems/validate-binary-search-tree/
题意
判断给定树是否是BST
思路
根据定义判断。递归。
代码
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(!root){return true;}
else{
return isBST(root)->validBST;
}
}
private:
struct TreeMes{
bool validBST;
int max;
int min;
};
TreeMes* isBST(TreeNode* root){
TreeMes *treeMes=new TreeMes;
if(!root->left&&!root->right){
treeMes->max=root->val;
treeMes->min=root->val;
treeMes->validBST=true;
return treeMes;
}
else if(!root->left){
TreeMes *rTreeMes=isBST(root->right);
if(rTreeMes->validBST&&(rTreeMes->min>root->val)){
treeMes->min=root->val;
treeMes->max=rTreeMes->max;
treeMes->validBST=true;
}
else{treeMes->validBST=false;}
delete rTreeMes;
return treeMes;
}
else if(!root->right){
TreeMes *lTreeMes=isBST(root->left);
if(lTreeMes->validBST&&(lTreeMes->max<root->val)){
treeMes->max=root->val;
treeMes->min=lTreeMes->min;
treeMes->validBST=true;
}
else{treeMes->validBST=false;}
delete lTreeMes;
return treeMes;
}
else{
TreeMes *lTreeMes=isBST(root->left);
TreeMes *rTreeMes=isBST(root->right);
if(lTreeMes->validBST&&(lTreeMes->max<root->val)&&rTreeMes->validBST&&(rTreeMes->min>root->val)){
treeMes->min=lTreeMes->min;
treeMes->max=rTreeMes->max;
treeMes->validBST=true;
}
else{treeMes->validBST=false;}
delete lTreeMes;
delete rTreeMes;
return treeMes;
}
}
};
[LeetCode_98]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 ...
- 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) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- PHP 使用非对称加密算法(RSA)
解释: 非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey).公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密:如果用私有密 ...
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...
- JVM jstack 详解
https://blog.csdn.net/zxp_cpinfo/article/details/54971115 输出到文件 >jstack -l PID >> /root/123 ...
- greenplum
参考文章:在linux系统上安装Greenplum数据库 https://blog.csdn.net/mingli_a/article/details/78779189 Greenplum安装步骤 ...
- jetty 入门
jetty因其能作为内嵌的应用服务器,随应用一起存在,在小批量应用中很受欢迎. jetty作为应用服务器: jetty下载: 在官网下载jetty:http://www.eclipse.org/jet ...
- vue:图片切换动态显示
<img :src="切换条件 ? require('xxx.png') : require('xxx.png')" />
- 对于两个初始时设置为Sensor的刚体,不会触发preSolve和postSolve
Main.as package{ import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamic ...
- php运行代码流程和性能优化方法
---恢复内容开始--- php文件->扫描->zd引擎去理解->opcodes->执行->输出 例子,用white随机循环20000数据进行性能测试,分别对比isset ...
- 侧边栏收起展开效果,onmouseover,onmouseout
//方法一<!doctype html> <html lang="en"> <head> <meta charset="UTF- ...
- 29.Junit测试框架.md
目录 作用 使用 单个对象的测试 有步骤的测试 注意 作用 用于简化测试,可以对方法,类,包等范围测试 使用 单个对象的测试 在需要测试的方法上加注解@Test,选中方法,运行里选择junit执行 同 ...