【LeetCode】98. Validate Binary Search Tree

时间:2021-10-02 09:29:59

题目:

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

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

提示:

这道题需要知道一个二叉搜索树的特性,就是一个二叉搜索数的中序遍历结果是一个严格单调递增序列。在知道了这个性质之后,就很好解了,这里我们给出非递归和递归两种解法。

代码:

非递归:

/**
* 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) {
return true;
}
inOrder(root);
for (int i = ; i < v.size(); ++i) {
if (v[i] <= v[i-]) {
return false;
}
}
return true;
} void inOrder(TreeNode* node) {
if (!node) {
return;
}
inOrder(node->left);
v.push_back(node->val);
inOrder(node->right);
} private:
vector<int> v;
};

用了一个额外的vector存储中序遍历的结果,看上去好像不是太理想,再看一下递归方法:

/**
* 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) {
TreeNode* pre = nullptr;
return isValidBST(root, pre);
} bool isValidBST(TreeNode *node, TreeNode*& pre) {
if (!node) {
return true;
}
if (!isValidBST(node->left, pre)) {
return false;
}
if (pre && node->val <= pre->val) {
return false;
}
pre = node;
return isValidBST(node->right, pre);
}
};

代码简单了很多,其实遍历的时候还是按照中序的思路来的,但是由于pre指针在函数间传递的过程当中指向的位置会发生改变,因此需要注意在函数参数那里需要将其写为指针的引用。

【LeetCode】98. Validate Binary Search Tree的更多相关文章

  1. 【LeetCode】98&period; Validate Binary Search Tree &lpar;2 solutions&rpar;

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

  2. 【LeetCode】98&period; Validate Binary Search Tree 解题报告(Python & C&plus;&plus; & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  3. 【一天一道LeetCode】&num;98&period; Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】99&period; Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

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

  6. 【LeetCode】 99&period; Recover Binary Search Tree &lbrack;Hard&rsqb; &lbrack;Morris Traversal&rsqb; &lbrack;Tree&rsqb;

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. 【Lintcode】095&period;Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  8. 【LeetCode】1008&period; Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  9. LeetCode OJ 98&period; Validate Binary Search Tree

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

随机推荐

  1. PHPer不能不看的50个细节&excl;

    1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...

  2. python os&period;system&lpar;&rpar;返回值判断

    最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...

  3. systemd添加自定义系统服务设置自定义开机启动

    1.服务权限 systemd有系统和用户区分:系统(/user/lib/systemd/system/).用户(/etc/lib/systemd/user/).一般系统管理员手工创建的单元文件建议存放 ...

  4. 多玩YY聊天记录解析全过程

    再来一发,现在开始! 下载安装YY,观察YY目录,很明显的发现了sqlite3.dll,这个数据库很多很多很多软件都在用,简单小巧且开源.删除sqlite3.dll 进入YY,历史记录不能正常显示,基 ...

  5. windows&period;onload和 document&period;ready区别

    在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...

  6. (6综合实验)从零开始的嵌入式图像图像处理&lpar;PI&plus;QT&plus;OpenCV&rpar;实战演练

    从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...

  7. python爬虫学习之爬取全国各省市县级城市邮政编码

    实例需求:运用python语言在http://www.ip138.com/post/网站爬取全国各个省市县级城市的邮政编码,并且保存在excel文件中 实例环境:python3.7 requests库 ...

  8. qcom wlan kernel 解读 WCNSS&lowbar;qcom&lowbar;cfg&period;ini 文件

    CORE/HDD/src/wlan_hdd_main.c 模块初始化: static int __init hdd_module_init ( void) { return hdd_driver_in ...

  9. &lbrack;glog&rsqb;&lowbar;&lbrack;C&sol;C&plus;&plus;&rsqb;&lowbar;&lbrack;使用glog来记录日志&rsqb;

    glog 快速使用教程 场景 1.大部分程序由函数组成, 每个函数执行一段设计好的逻辑, 但是大部分的时候有可能出现意料之外的值, 这时候就很想知道这种意料以外的值是如何产生的, 这就需要一个函数调用 ...

  10. webpack构建react多页面应用

    写这个的初衷是很难找一个简洁的项目脚手架,很多脚手架都有很多依赖,光看依赖就要很久,所以自己参照网上的内容,弄个这么一个简单的多页面的脚手架. 利用creat-react-app 新建一个react应 ...