LeetCode 98. Validate Binary Search Tree–C++解法–判断是否是BST–递归,迭代做法,中序遍历


LeetCode题解专栏:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结
大部分题目C++,Python,Java的解法都有。


题目地址:Validate Binary Search Tree - LeetCode


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.

Example 1:

    2/ \1   3Input: [2,1,3]
Output: true

Example 2:

    5/ \1   4/ \3   6Input: [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

这道题目是判断二叉树是否是搜索二叉树,自然就想到了递归和迭代2种做法。

递归的话在考虑后发现对每个节点都要维护一个最小值和最大值。
C++解法如下:

/*** 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) {return isValidBST(root, nullptr, nullptr);}bool isValidBST(TreeNode *root, TreeNode *min, TreeNode *max) {if (!root) return true;if (min != nullptr && root->val <= min->val) return false;if (max != nullptr && root->val >= max->val) return false;return isValidBST(root->left, min, root) && isValidBST(root->right, root, 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;stack<TreeNode*> s;TreeNode* current = root; TreeNode* prev = nullptr;while (current || !s.empty()){while(current){s.push(current);current = current->left;}current = s.top(); s.pop();if (prev && prev->val >= current->val) return false;prev = current;current = current->right;} return true;}
};

奇怪的是中序遍历花费的时间跟递归做法花费的时间基本一样。

LeetCode 98. Validate Binary Search Tree--C++解法--判断是否是BST--递归,迭代做法,中序遍历相关推荐

  1. LeetCode --- Validate Binary Search Tree

    题目链接 判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST 如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况) 附上代码: 1 /** 2 * De ...

  2. 98. Validate Binary Search Tree

    不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 题目要求判断树是否为二叉搜索树.要求为:1.一个节点的左子树的所有节点均小于该节点:2.一个节点的右子树上的所有节点 ...

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

  4. LeetCode 99. Recover Binary Search Tree

    LeetCode 99. Recover Binary Search Tree 刚看到这个题真是一脸懵逼啊... 博客转载自:http://www.cnblogs.com/grandyang/p/42 ...

  5. LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)

    Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...

  6. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法

    题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...

  7. LeetCode:Validate Binary Search Tree

    题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  8. LeetCode OJ - Recover Binary Search Tree

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  9. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

最新文章

  1. 25%的游戏通过Steam支持Linux系统
  2. 2017年09月23日普级组 数列
  3. XP下如何恢复Administrator
  4. PyQt5 QTreeWidget更改item项前的展开折叠三角图标
  5. VS2008无法下断点调试的若干解决办法
  6. 五、RabbitMQ管理与高可用集群
  7. 微信小程序开发--【初体验】(一)
  8. C#中is、as以及强制转换之间区别
  9. Java实现在线视频通话
  10. 人口matlab数学模型,基于MATLAB构建人口数学模型研究二胎开放对中国人口的影响...
  11. 开发一款APP都有哪些流程?
  12. 【视频学习】宋维钢词霸天下38000词汇速记 万法归宗之英语语法速成全集
  13. 如果局域网当中两台电脑互相ping不通
  14. GTX1050Ti和GTX1060显卡哪个好?
  15. 用Python实现一个简易的“听歌识曲”demo(一)
  16. 网络与信息安全学习日记
  17. opencv-python 去除图片文字
  18. 引入mybatis-plus报 Invalid bound statement错误怎么办,动动手指改一个地方就行
  19. 简易钓鱼网站的构建(Kali SetoolKit)
  20. python自己做课程表_Python 大学生课表 iCalendar (.ics) 生成

热门文章

  1. centos7 选定默认启动内核,及删除无用内核
  2. angularjs php上传文件,AngularJS 文件上传 的功能你了解的多少?几分钟就让你了解angularjs的文件上传...
  3. Nature会议:驾驭植物微生物组(21年10月22-24,在线,优惠截止9月24日)
  4. Science:致病菌激活根系内生微生物组抵抗病害的功能
  5. 华南农业大学在Annual Review of Microbiology发表三篇综述文章
  6. 如何快速评估16S rRNA基因引物的覆盖率及特异性
  7. R语言构建logistic回归模型并评估模型:模型预测结果抽样、可视化模型分类预测的概率分布情况、使用WVPlots包绘制ROC曲线并计算AUC值
  8. R绘制发散型条形图(Diverging Bars)
  9. 顺序特征选择器(SequentialFeatureSelector (SFS))
  10. AdaBoostClassifer详解及实战