题目

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.

confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

OJ’s Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

Here’s an example:

分析

一倒判断给定二叉树是否为二叉查找树的题目。

看似一道很简单的题目,愣是让我提交了4次才AC。

都卡在了int数据类型溢出问题上了!!! 每每涉及到INT_MIN和INT_MAX的题目都很是头疼。最终还是改变了策略。

/*

* 需要注意的是,左子树的所有节点都要比根节点小,

* 而非只是其左孩子比其小,右子树同样。

*/

//二叉查找树的一个特点就是其中序遍历结果为一个递增序列,可作为用来判断

这个简单的判断规则,开始竟没想到,败在了递归判断!

关于这道题目,这篇博文总结的很好,博文链接

AC代码

class Solution {
public:
/*
* 需要注意的是,左子树的所有节点都要比根节点小,
* 而非只是其左孩子比其小,右子树同样。
*/
bool isValidBST(TreeNode* root) {
if (!root)
return true; //二叉查找树的一个特点就是其中序遍历结果为一个递增序列,可作为用来判断
InOrder(root); int size = ret.size();
for (int i = 0; i < size - 1; ++i)
{
if (ret[i] >= ret[i + 1])
return false;
}//for
return true;
} //中序遍历二叉查找树
void InOrder(TreeNode *root)
{
if (!root)
return; InOrder(root->left);
ret.push_back(root->val);
InOrder(root->right); } private:
vector<int> ret;
};

GitHub测试程序源码

LeetCode(98) Validate Binary Search Tree的更多相关文章

  1. LeetCode(99) Recover Binary Search Tree

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

  2. LeetCode之“树”:Validate Binary Search Tree

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

  3. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  4. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  5. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

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

  8. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. Tinghua Data Mining

    Learning Resources 书籍: 期刊: 业界先驱: 开阔视野,掌握业界最新动态. 工具: 数据挖掘是很多学科的综合体: 甭管叫什么名字,归根到底都是数据挖掘: Comprehensive ...

  2. bzoj1538 [NWERC2017]High Score

    网上的题解都很奇怪.. 经过相当长时间的思考,有了一个有效(自认为)的解法 设某一种合法分配方案完成后三个数分别变为a,b,c,其中a>=c,b>=c 此时如果让c减1,让a或b加1(设让 ...

  3. Jmeter(二十三)稳定性测试后的波形图

    jmeter-plugins.org 这个网站为 JMeter 提供了一些增强型功能的插件,使用起来就像 Eclipse 装插件一样,完全做到了插件的可插拔特性.本文简要介绍如何使用这些插件让你的 J ...

  4. 关于@Mapper和@Repository的一点小理解

    参考博客:https://blog.csdn.net/lalioCAT/article/details/51803461 如果在接口上@Mapper,然后再在 xml中的namespace指向mapp ...

  5. Unity Shader入门精要学习笔记 - 第11章 让画面动起来

    转自 冯乐乐的 <Unity Shader入门精要> Unity Shader 中的内置变量 动画效果往往都是把时间添加到一些变量的计算中,以便在时间变化时画面也可以随之变化.Unity ...

  6. mysql查询某个数据库某个表的字段

    1.查看字段详细信息 -- 查看详细信息 SELECT COLUMN_NAME "字段名称", COLUMN_TYPE "字段类型长度", IF(EXTRA=& ...

  7. IT人怎样防止过劳死?如何成为时间的主人?

    投行的朋友还没走几天,搜狐的一位同胞又去了.又是过劳死!    每当读到这类新闻,IT人无不反镜自照,顾影自怜.无法拼爹拼钱的我们,似乎只有拼命了.生活好惨淡啊!    有人说:年轻人,悠着点儿!立刻 ...

  8. 关于搭建系统直播和Thinkphp的杂谈(持续更新)

    Q:Access denied for user 'root'@'localhost' 错误 A:第一种:配置文件中把数据库的用户名密码再改一遍,把runtime里的文件删除  第二种:修改syste ...

  9. 重置Cacti密码

    Cacti登录密码忘记,重置Cacti密码 用root用户进入系统 [root@localhsot]# mysql -u root -p mysql> show databases; mysql ...

  10. windows命令行快速启动软件

    windows桌面上太多的应用程序快捷方式很影响美观,于是寻思使用类似Linux系统中命令行的方式来启动软件. 只需要3步: 1.建立一个目录A,用来存放快捷方式.比如,建立D:\path.并复制快捷 ...