题目链接

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的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

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

  2. Validate Binary Search Tree

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

  3. 【leetcode】Validate Binary Search Tree

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

  4. LintCode Validate Binary Search Tree

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

  5. 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 ...

  6. [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 ...

  7. 【LeetCode练习题】Validate Binary Search Tree

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

  8. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  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. 【Noip模拟 20161005】公约数

    问题描述 小ww最近仔细研究了公约数,他想到了以下问题:现有nn个正整数,从中选k(2≤k≤n)k(2≤k≤n) 个,设这kk个数的最大公约数为gg,则这kk个数的价值为k×gk×g.求这个价值的最大 ...

  2. js 迭代器 解说

    这里要说的是迭代器,是一种思路而已,代码相对来不是最关键的,个人认为,最关键的部分是实现的思路 要求: 在一个网页中,将所有的 p 元素的内容进行替换,但是在特定的位置的 p 元素是要有差异的进行替换 ...

  3. HashSet和LinkedHashSet解析

    一.简介 1.Set概念 Set可以理解为集合,非常类似数据概念中的集合,集合三大特征:1.确定性:2.互异性:3.无序性,因此Set实现类也有类似的特征. 2.HashSet HashSet继承自A ...

  4. Kotlin语言编程技巧集

    空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...

  5. [Shell]Bash基本功能:历史命令 & 别名 & Bash快捷键

    /*----------------------------------------------------------------------------------------------- @黑 ...

  6. week06 07 创建RPC SERVER 换个镜像安装下载

    RPC server 使用python类库 https://pypi.org/project/python-jsonrpc/ 和NPM 不一样 他没有global选项 他安装的就是全局的安装的类库叫p ...

  7. 我理解的BFC(块级格式化上下文)

    BFC(Block formatting context) 直译为"块级格式化上下文". BFC它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Bl ...

  8. 使用AutoMapper时Expression的转换

    此文章为转载:http://www.bubuko.com/infodetail-699735.html 参考链接: http://q.cnblogs.com/q/34480/   dudu有回复,其中 ...

  9. Swift类型转换 和 类型别名的定义(typealias)

    (一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...

  10. MySQL 服务无法启动

    拷贝的mysql数据库配置文件的路径网络更改