【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 ...
随机推荐
- VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值
E. Rooks and Rectangles Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemse ...
- Codeforces Round #300 B. Quasi Binary 水题
B. Quasi Binary Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/probl ...
- malloc和calloc的差别
做C这么久了,才了解calloc函数也是挺丢人的. 从网上找了非常多关于这两者差别的文章.有的甚至总结了好多人的结论.但我感觉都没有说的非常明确. 当中关于函数原型的差别根本就不是必需再讨论了,是个人 ...
- [前端]使用JQuery UI Layout Plug-in布局
引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...
- 第一章 Actionscript学习基本知识笔记及flashdevelop软件的安装问题
OOP:封装.继承.多态. Pubilc :完全公开. Internal:包内类成员可以互相访问. Private:仅当前类可以访问. Protected:当前类和当前类的子类可以访问. 被关键词fi ...
- TeX系列: MATLAB和LaTeX结合绘图
目的是在MATLAB中绘图,在LaTeX中利用PGFPlots实现对图的修饰,比如坐标系.坐标轴标记.标题等等.这样能够保证图的中标记与正文文本的一致性,同时又可利用MATLAB强大的数据处理和丰富的 ...
- Spring Bean 注入 2 注解篇
1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...
- Orchard运用 - 特定主题添加独立代码文件
今天继续跟大家分享捣鼓Orchard的一些心得.其实有时一些问题或者Bugs还是蛮好解决的,主要看你采取哪种方式方法.比如有时我们为了扩展某些特性或功能,你可以搭建一个全新的模块来完成,如果临时的或简 ...
- go语言基础之回调函数
1.回调函数 示例: 实现:加,减,乘 示例: package main import "fmt" type FuncType func(int, int) int //实现加法 ...
- Servlet学习笔记(一):生命周期
一.Servlet 生命周期: Servlet 生命周期可被定义为从创建直到毁灭的整个过程.以下是 Servlet 遵循的过程:初始化——响应请求——终止——回收 Servlet 通过调用 init ...