【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree
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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
vector<int> ret;
inOrder(root, ret);
if(ret.size() <= )
return true;
for (int i = ; i < ret.size(); i ++)
{
if(ret[i] <= ret[i-])
return false;
}
return true;
}
void inOrder(TreeNode *root, vector<int>& ret)
{
if(!root)
return;
if(root->left)
inOrder(root->left, ret);
ret.push_back(root->val);
if(root->right)
inOrder(root->right, ret);
}
};

解法二:
由于只需要关心前一个节点与当前的大小关系,因此不用保存所有值。
只需要使用一个全局变量保存前一个节点值即可。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
TreeNode* last = NULL;
bool isValidBST(TreeNode *root)
{
if(root == NULL)
return true;
else
{
bool leftRes = isValidBST(root->left);
//short cut
if(leftRes == false)
return false;
if(last && last->val >= root->val)
return false;
last = root;
return isValidBST(root->right);
}
}
};

【LeetCode】98. Validate Binary Search Tree (2 solutions)的更多相关文章
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【LeetCode】98. Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode OJ 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 ...
随机推荐
- sklearn中的投票法
投票法(voting)是集成学习里面针对分类问题的一种结合策略.基本思想是选择所有机器学习算法当中输出最多的那个类. 分类的机器学习算法输出有两种类型:一种是直接输出类标签,另外一种是输出类概率,使用 ...
- ajax拖拽上传文件
AJAX拖拽上传 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 如何删除Struts2动作的后缀扩展名
在Struts2中,所有动作类有一个默认的后缀 .action 扩展. 例如, <struts> <package name="default" namespac ...
- hihoCoder - 1082 - 然而沼跃鱼早就看穿了一切 (字符串处理!!)
#1082 : 然而沼跃鱼早就看穿了一切 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽全 ...
- linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)
转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...
- vagrant多节点配置
1.vagrantfile的配置 Vagrant.configure("2") do |config| config.vm.box = "xinjieLinux" ...
- 推荐两份学习 Kotlin 和机器学习的资料
最近 Kotlin 和人工智能比较火,有不少同学留言问我怎么学习 Kotlin,怎么学习机器学习,今天就给大家推荐两份不错的学习资料. 1. Kotlin 学习资料其实,在我看来最好的学习资料就是 K ...
- mac的dns缓存查询和清除
1.清楚dns缓存 dscacheutil -flushcache 2.查询nslookup
- SPSS Clementine 数据挖掘入门2
下面使用Adventure Works数据库中的Target Mail作例子,通过建立分类树和神经网络模型,决策树用来预测哪些人会响应促销,神经网络用来预测年收入. Target Mail数据在SQL ...
- Dockerfile命令
Dockerfile分基础镜像信息.维护者信息.镜像操作指令.容器启动时执行指令 FROM 镜像名:标签 第一条指令必须时FROM MAINTAINER 维护者信息 RUN command或者RUN ...