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) ...
随机推荐
- SNMP4J 总结
一.SNMP4J介绍 SNMP4J是一个用Java来实现SNMP(简单网络管理协议)协议的开源项目.它支持以命令行的形式进行管理与响应.SNMP4J是纯面向对象设计与SNMP++(用C++实现SNMP ...
- Linux之文本处理命令
Sort 将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. -u 在输出行中去除重复行 -r 改为降序(默认升序) ...
- attribute与property区别总结
在前阵子看JQuery源码中,attr()的简单理解是调用了element.getAttribute()和element.setAttribute()方法,removeAttr()简单而言是调用ele ...
- html5拖动滑块
html5中input有增加type=range.这为拖动滑块提供了很大的便利.下面是他的属性: <!DOCTYPE html> <html lang="en"& ...
- top 进程管理
top 动态查看进程 前五行解释: 第一行参数说明: top - 07:06:19 当前时间 up 10 min, 系统运行时间,格式为时:分 1 user, 当前登录用户数 load av ...
- 解决win64无法添加curl扩展的问题
网上试了很多方法都无效,最后直接用phpstudy集成安装包中对应版本的php_curl.dll替换即可
- openssl 安装配置
Openssl是个为网络通信提供安全及数据完整性的一种安全协议,囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用.首先下载Openssl包: ...
- Linux shell例子
#!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...
- SQL Server2005的数据还原与备份具体步骤
一:备份数据库步骤 1. 第一步:在开始—>程序(P)—>MicrosoftSQLserver2005—>SQLServerManagementStudio(如下图) 2. 第二步: ...
- rhythmbox插件开发笔记2:背景知识学习 D-Bus&VFS&Gio& Python GTK+ 3
这次主要简单介绍下相关的背景知识 D-Bus&VFS&Gio& Python GTK+ 3 D-Bus D-Bus是开源的进程通信(IPC)系统,它允许多个进程进行实时通信. ...