【问题】给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

示例 :
输入: / \ 输出: true
示例 :
输入: / \ / \ 输出: false
解释: 输入为: [,,,null,null,,]。
根节点的值为 ,但是其右子节点值为 。

【思路】如何判断一棵二叉树是否为BST,很简单的思路就是:对这棵二叉树进行中序遍历,然后判断其中序遍历后的序列是不是单调递增的序列,如果是,则为一棵BST,否则不是。

但是二叉树的中序遍历有两个版本,递归版和非递归版本,我们先来看递归版本,其实际就是一个dfs算法,从根节点依次向下深入,在递归体内我们需要设置两个变量min, max来进行数值边界的判断,以使得遍历后的序列为一个单调增序列!

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* root, long int mi, long int ma){
if(root == nullptr){
return true;
}
if(root->val <= mi || root->val >= ma) return false;
else return dfs(root->left, mi, root->val) && dfs(root->right, root->val, ma); } bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
return dfs(root, INT64_MIN, INT64_MAX);
}
};

我们还可以使用一个堆栈来实现二叉树的费递归版的中序遍历!!!

/**
* Definition for a binary tree node.
* 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 == nullptr) return true;
TreeNode* pre = nullptr;
TreeNode* cur = root;
stack<TreeNode*> sta;
while(!sta.empty() || cur != nullptr){
if(cur != nullptr){
sta.push(cur);
cur = cur->left;
}else{
cur = sta.top();
sta.pop();
if(pre && cur->val <= pre->val) return false;
pre = cur;
cur = cur->right;
}
}
return true;
}
};

【LeetCode】验证二叉搜索树的更多相关文章

  1. LeetCode - 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. ...

  2. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

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

  4. LeetCode:验证二叉搜索树【98】

    LeetCode:验证二叉搜索树[98] 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当 ...

  5. LeetCode初级算法--树02:验证二叉搜索树

    LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  6. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  7. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. LeetCode(98): 验证二叉搜索树

    Medium! 题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右 ...

  9. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  10. LeetCode 98. 验证二叉搜索树 | Python

    98. 验证二叉搜索树 题目来源:https://leetcode-cn.com/problems/validate-binary-search-tree 题目 给定一个二叉树,判断其是否是一个有效的 ...

随机推荐

  1. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  2. 几种编辑器的markdown-toc生成目录在github上的表现

    Vscode vscode的markdown-toc插件的实现是比较好的, 目前发现的问题就只有在自动生成带链接目录的时候无法正确识别和生成一些特殊的字符. 例如: ▶ 这导致在标题中不能加入特殊字符 ...

  3. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  4. JuJu团队12月28号工作汇报

    JuJu团队12月28号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 飞飞 完成GUI 待安排 无 婷婷 调试代码 提升acc 无 恩升 修正evaluate 修正evalu ...

  5. HiBench成长笔记——(4) HiBench测试Spark SQL

    很多内容之前的博客已经提过,这里不再赘述,详细内容参照本系列前面的博客:https://www.cnblogs.com/ratels/p/10970905.html 和 https://www.cnb ...

  6. 【pwnable.kr】 [simple login]

    Download : http://pwnable.kr/bin/login Running at : nc pwnable.kr 9003 先看看ida里面的逻辑. 比较重要的信息时input变量再 ...

  7. vue :src 不显示的解决方案

    一定要将静态资源引入 [ require("@/assets/") ],绑定到 模型绑定的:src 数据中 动态的数据才能有效   <template>   <d ...

  8. mongodb - 关联字段

    1,博客表结构  Blog.js var mongoose = require('mongoose') mongoose.connect('mongodb://localhost/test',{ us ...

  9. centos7 程序快捷方式

    /usr/share/Applications 在此文件夹中,全是".desktop"的文件,利用终端打开一个看看就知道了!

  10. 034、Java中自增之++在前面的写法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...