LeetCode(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 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;
};
LeetCode(98) Validate Binary Search Tree的更多相关文章
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- LeetCode之“树”:Validate Binary Search Tree
题目链接 题目要求: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【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) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- bzoj 4597||洛谷P4340 [Shoi2016]随机序列
https://www.lydsy.com/JudgeOnline/problem.php?id=4597 https://www.luogu.org/problemnew/show/P4340 妄图 ...
- Java EE学习笔记(六)
初识MyBatis 1.MyBatis的定义 1).MyBatis(前身是iBatis)是一个支持普通SQL查询.存储过程以及高级映射的持久层框架. 2).MyBatis框架也被称之为ORM(Obje ...
- nodejs express 设置html后缀模板
express 框架的默认渲染模板的后缀是 ejs ,由于编译器在ejs的文件里写html代码没有高亮显示,所以使用html模板. 示例: var app = express(); app.set(' ...
- android开发学习 ------- 【转】 android中的线程池
线程很常见 , https://blog.csdn.net/seu_calvin/article/details/52415337 参考,保证能看懂.
- 关于h5中背景音乐的自动播放
音乐的自动播放属性,这里也介绍一下: <audio controls="controls" autoplay="autoplay"> <sou ...
- SourceInsight主题设置
自己经常忘记怎样设置SourceInsight主题,这次一定要记住! 0. 退出SourceInsight软件1. 替换配置文件操作:拷贝Global.CF3到“我的文档\Source Insight ...
- cp参数详解
-a 相当于pdr的意思 -d 若原文件为连接文件,则复制链接文件属性,而非文件本身 -f 强制复制,有重复时,不询问用户,而直接强制复制 -i 目标文件存在的话,先询问 -p 与文件的属性一起复制 ...
- 闹心的CSDN
近来搜索技术文章时,每次来到csdn上时,显示全文就提示登陆. 唉登陆就登陆吧,记不清账号了,就用手机号获取验证码.然后更改密码.我靠,密码居然要8位以上,要有大小写字母.数字和标点符号的组合... ...
- shell框架
#!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...
- 运行spark自带的例子出错及解决
以往都是用java运行spark的没问题,今天用scala在eclipse上运行spark的代码倒是出现了错误 ,记录 首先是当我把相关的包导入好后,Run,报错: Exception in thre ...