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)的更多相关文章

  1. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

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

  2. 【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 ...

  3. 【一天一道LeetCode】#98. Validate Binary Search Tree

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

  4. 【LeetCode】99. 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. 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 ...

  7. 【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 ...

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

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

  9. 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 ...

随机推荐

  1. 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 ...

  2. Codeforces Round #300 B. Quasi Binary 水题

    B. Quasi Binary Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/probl ...

  3. malloc和calloc的差别

    做C这么久了,才了解calloc函数也是挺丢人的. 从网上找了非常多关于这两者差别的文章.有的甚至总结了好多人的结论.但我感觉都没有说的非常明确. 当中关于函数原型的差别根本就不是必需再讨论了,是个人 ...

  4. [前端]使用JQuery UI Layout Plug-in布局

    引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...

  5. 第一章 Actionscript学习基本知识笔记及flashdevelop软件的安装问题

    OOP:封装.继承.多态. Pubilc :完全公开. Internal:包内类成员可以互相访问. Private:仅当前类可以访问. Protected:当前类和当前类的子类可以访问. 被关键词fi ...

  6. TeX系列: MATLAB和LaTeX结合绘图

    目的是在MATLAB中绘图,在LaTeX中利用PGFPlots实现对图的修饰,比如坐标系.坐标轴标记.标题等等.这样能够保证图的中标记与正文文本的一致性,同时又可利用MATLAB强大的数据处理和丰富的 ...

  7. Spring Bean 注入 2 注解篇

    1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...

  8. Orchard运用 - 特定主题添加独立代码文件

    今天继续跟大家分享捣鼓Orchard的一些心得.其实有时一些问题或者Bugs还是蛮好解决的,主要看你采取哪种方式方法.比如有时我们为了扩展某些特性或功能,你可以搭建一个全新的模块来完成,如果临时的或简 ...

  9. go语言基础之回调函数

    1.回调函数 示例: 实现:加,减,乘 示例: package main import "fmt" type FuncType func(int, int) int //实现加法 ...

  10. Servlet学习笔记(一):生命周期

    一.Servlet 生命周期: Servlet 生命周期可被定义为从创建直到毁灭的整个过程.以下是 Servlet 遵循的过程:初始化——响应请求——终止——回收 Servlet 通过调用 init ...