题目链接

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. ubuntu卸载福昕阅读器

    在安装目录找到maintenancetool.sh运行之 ~/opt/foxitsoftware/foxitreader

  2. URL记录

    http://orchome.com/5https://www.cnblogs.com/haozhengfei/p/2192231596ceb2ac4c22294dbd25a1ca.htmlhttps ...

  3. Shell编程:小白初步

    shell类型: shell的历史网络上有一大堆,这里就不介绍了.但是我们的Linux系统上是有许多种shell的我们可以查看:使用命令 vi /etc/passwd 可以查看用户对应的shell(其 ...

  4. 10:处理 json

    json 通用的数据类型, 所有的语言都认识.json 是字符串.key-value 必须使用双引号1. loads() 和 dumps() 的使用 json.loads() 将 json 字符串转换 ...

  5. UNITY 多个子MESH与贴图的对应关系

    多个贴图时,与子MESH一一对应,如果多了 ...还待研究如何对应

  6. Spark安装过程纪录

    1 Scala安装 1.1 master 机器 修改 scala 目录所属用户和用户组. sudo chown -R hadoop:hadoop scala 修改环境变量文件 .bashrc , 添加 ...

  7. javaweb 学习系列【转】

    http://www.cnblogs.com/xdp-gacl/category/574705.html jsp指令 http://www.cnblogs.com/huiyuantang/p/5332 ...

  8. SQL Server 2008读取域帐户信息

    参考:http://www.pawlowski.cz/2011/04/querying-active-directory-sql-server-t-sql/ 1.建立  link server . u ...

  9. 总是Eqw

    1.投递总是Eqw状态 qstat -j job_ID #Eqw状态的job id qconf -sq all.q |grep host qconf -shgrp @allhosts

  10. SpringJDBC数据库的基本使用

    SpringJDBC的基础使用部分内容 云笔记项目数据库部分采用的是Spring-MyBatis,前面学过了JDBC,SpringJDBC,Mybatis和Spring-MyBatis,有必要重新复习 ...