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) ...
随机推荐
- Hive_Hive和数据仓库简介
文章摘自 : http://www.imooc.com/video/7573 Hive是建立在Hadoop HDFS上的数据仓库基础架构.Hive可以用来进行数据的ETL.Hive定义了简单的类似SQ ...
- 18113 Secret Book of Kungfu 按位DFS
http://acm.scau.edu.cn:8000/uoj/mainMenu.html 18113 Secret Book of Kungfu 该题有题解 时间限制:1000MS 内存限制:65 ...
- awk一些简单命令
最简单地说, AWK 是一种用于处理文本的编程语言工具.AWK 在很多方面类似于 shell 编程语言,尽管 AWK 具有完全属于其本身的语法. 尽管操作可能会很复杂,但命令的语法始终是: awk ' ...
- Oracle 恢复数据后,数据库中中文变成问号解决方法
1.右击---我的电脑---环境变量 2.新增环境变量 变量名:LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 3.重启PLSQL或 ...
- office 导出问题
就用程序池右击项目高级设置 应用程序池的项目中的标识改为 LocalSystem 启用32位应用程序设为true或false
- deepin15.2无线网无法使用
原文链接:https://bbs.deepin.org/forum.php?mod=viewthread&tid=40276&highlight=%E6%97%A0%E7%BA%BF% ...
- centos6.3下postgresql-9.3安装记录
Xshell for Xmanager Enterprise 4 (Build 0186) Copyright (c) 2002-2011 NetSarang Computer, Inc. All r ...
- RK3288开发过程中遇到的问题点和解决方法之Packages
去除桌面渐变黑边 Launcher3/.../Launcher.java SETWorkspaceBackground => setWorkspaceBackground 注释boolean i ...
- Notepad++设计Tab制表符为4个空格
Notepad++中,常常需要将一个Tab制表符转换成4个空格.这种情况大多发生在对空白检查严格的情况下,比如Python程序. 设置 → 首选项 → 制表符设置 → 勾选“ 转换为空格 ”.
- JS 语言基础
两个变量 相加 var s="今天下雨了"; var i=10; alert(i+s); 这里的i+s是拼接的意思 显示出来是 今天下雨了10 假设我改 s="2 ...