[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) ...
随机推荐
- nohup php -f xx.php &
nohup php -f xx.php &
- virtual 初探
两种代码方式: class person { public: void f() { cout << "person.f()" << endl; } }; c ...
- gevent mysql
使用gevent实现mysql并发时,每个greenlet应该独享一个mysql连接,否则,不同的greenlet之间会相互影响. ultramysql doesn't allow you to ma ...
- kvm配置USB直通
参照:https://www.linuxidc.com/Linux/2014-12/110919.htm WebVirMgr界面是没有直接的途径了,只能靠修改xml文件,在<device> ...
- 安装vue CLI后, 出现安装权限问题
问题:安装vue CLI后,出现:npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/usr/l ...
- Ubuntu网卡配置
目录 1.查看所有可用网卡 2.编辑配置文件 3.添加可用网卡信息 4.重启网络服务 5.查看网卡信息 1.查看所有可用网卡 $ ifconfig -a # -a display all interf ...
- 通过使用浏览器对象模型,输出当前浏览器窗口中打开的文档的URL信息,并将显示在窗口中。
<script type="text/javascript">window.document.write("这个网页文件来自:".bold());w ...
- 【原】Win7 host 与 virtualbox ubuntu guest 相同ping通
环境:Win7 host 与 virtualbox ubuntu guest 在 “设置”-->"网络" 中选择网卡1,修改连接方式为 “桥接网卡” 后, 主机和虚拟机可以相 ...
- (转) 为什么选择.NETCore?
https://www.cnblogs.com/xiaoliangge/p/8373100.html 为什么选择.NETCore? 为什么选择.NETCore? 学习新的开发框架是一项巨大的投资 ...
- set 转 enumeration