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. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. win2003服务器装spl2008,打安全补丁后无法进入SQL Server Management Studio

    解决方法就是:卸载垃圾的360安全卫士,用windows自带的更新工具更新系统补丁,就好了

  3. 64位系统下同时使用64位和32位的eclipse

    eclipse.ini 文件使用说明 The -vm option and its value (the path) must be on separate lines. The value must ...

  4. 汇编语言学习笔记(十二)-浮点指令----ACM

    http://blog.csdn.net/q_l_s/article/details/54909328

  5. Small tips of APP H5 page

    在开发日常落地页的时候,每当碰到一些很酷炫的宣传图用css实现很复杂且耗时的时候,一般采取切图然后将其放在页面中,在这个过程中发现<img/>标签中图片下方会有一行小空白,影响了与后一部分 ...

  6. 使用代码配置 NHibernate

    多数情况下 NHibernate 使用配置文件进行配置, 但是我们也可以使用代码进行配置, 步骤如下: 1. 创建一个 Configuration using Nhibernate.cfg; var ...

  7. hadoop下c++程序-天气实例

    非常希望能在hadoop上做c++程序.自己对c++还是有点情节的,依据<hadoop权威指南中文第二版>Hadoop的Pipes进行了试验,并測试成功 #include <algo ...

  8. windows服务的安装和卸载方法

    安装 创建“安装.bat”文件,用记事本打开此文件,内容为 c:\windows\microsoft.net\framework\v4.0.30319\InstallUtil.exe E:\progr ...

  9. java学习笔记9--内部类总结

    java学习笔记系列: java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Object类 java学习笔记5--类的方法 java学习笔记4--对 ...

  10. ES6 中的 Set

    收录待用,修改转载已取得腾讯云授权 作者:kurtshen ES6 新增了几种集合类型,本文主要介绍Set以及其使用. 其基本描述为 Set对象是值的集合,你可以按照插入的顺序迭代它的元素. Set中 ...