【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:1
/ \
2 3
Binary tree [1,2,3], return false.
(二)解题
题目大意:给定一个二叉树,判断该树是不是二叉搜索树。
解题思路:二叉搜索树满足,对于一个父节点,左子树所有节点的数值小于父节点的数值,右子树的所有节点的数值大于父节点的数值。
我的做法是采用递归的方法,并用一对min和max来记录左/右子树需要满足的数值范围。对于一个父节点root,需要满足在(min,max)范围内,然后判断左右节点,需要满足left
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root==NULL) return true;
return dfsValidBST(root,-2147483649,2147483648);//这里初始范围设为INT_MIN-1,INT_MAX+1
}
bool dfsValidBST(TreeNode* root , long min , long max)
{
if(root->val<=min||root->val>=max) return false;
bool left = true;
bool right = true;
if(root->left!=NULL)//left不为空
{
if(root->left->val < root->val) left = dfsValidBST(root->left,min,root->val);//满足条件继续往左子树搜索,min和max更新
else left = false;
}
if(root->right!=NULL)//right不为空
{
if(root->right->val > root->val) right = dfsValidBST(root->right,root->val,max);//满足条件继续往右子树搜索,min和max更新
else right = false;
}
return left&&right;//二者都为true,才返回true
}
};
【一天一道LeetCode】#98. Validate Binary Search Tree的更多相关文章
- [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 ...
- 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 ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [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 ...
- [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆
描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...
- 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 dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
随机推荐
- java 学习笔记2 面向对象(上)
类和对象 类是某一批对象的抽象,可以把类理解成某种概念.对象是一个具体存在的实体.类和对象是面向对象的核心. 类定义的是多个实例的特征,类不是具体存在,实例才是具体存在. 定义类(class)的语法: ...
- Java HashMap的扩容
最近博主参加面试,发现自己对于Java的HashMap的扩容过程理解不足,故最近在此进行总结. 首先说明博主德Java为1.8版本 HashMap中的变量 首先要了解HashMap的扩容过程,我们就得 ...
- ubuntu远程桌面连接命令rdesktop连接windows远程桌面详解
sudo apt-get install rdesktoprdesktop 124.42.120.174:1433 呵呵,连接成功了. -f 全屏-a 16位色默认端口是3389(linux 22 s ...
- 常见常用的CSS
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...
- UMAIL安装教程
UMAIL安装教程 一.UMAIL虚拟机的创建与安装 首先在VM中选择新建虚拟机 选择下一步 选择稍后安装操作系统 选择Linux系统 版本CentOS 7 64位 选择名称与安装位置 选择下一步 选 ...
- FileOutputStream&FileInputStream&异常的使用
FileOutputStream&FileInputStream&异常的使用 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真 ...
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...
- OLE:对象的类没有在注册数据库中注册
我在网上下载了破解版的SAS9.3,用了一段时间之后,今天打开就填出一个提示框:OLE:对象的类没有在注册数据库中注册 激活该对象所需的应用程序不可用.是否用"转换--"将其转换为 ...
- merge into的用法及10g新特性总结
merge into 的作用: 将源数据(来源于实际的表,视图,子查询)更新或插入到指定的表中(必须实际存在),依赖于on条件,好处是避免了多个insert 和update操作. merge是一个目标 ...
- Node.js URL
稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...