(BST 递归) leetcode98. 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 thanthe 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:
Input:
2
/ \
1 3
Output: true
Example 2:
5
/ \
1 4
/ \
3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
is 5 but its right child's value is 4.
-----------------------------------------------------------------------------------------
这个是一个Binary Search Tree的问题,即二叉搜索树。参考大佬的博客:http://www.cnblogs.com/grandyang/p/4298435.html 第一个解法:
这个题实际上是简化了难度,因为这个题说的BST指的是左 < 根 < 右,而不是 左 <= 根 < 右,这样的话就可以方便的使用中序遍历将所有值的节点保存到一个数组里,然后遍历这个数组,判断数组里面是否为升序(即a < b,不是 a <= b),然后返回false和true。
左 <= 根 < 右这个情况中
10 10
/ 和 \ 中,
10 10
用中序遍历最后得到的数组都是[10,10],[10,10],但是左边的是BST,而右边却不是BST,不好区分,如果去掉等号的话,就可以认为都不是BST,难度下降了不少。
C++代码:
/**
* 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) return true;
vector<int> vec;
inorder(root,vec);
for(int i = ; i < vec.size() - ; i++){
if(vec[i+] <= vec[i])
return false;
}
return true;
}
void inorder(TreeNode *root,vector<int> &vec){
if(!root) return;
inorder(root->left,vec);
vec.push_back(root->val);
inorder(root->right,vec);
}
};
第二个解法:
根据Grandyang大佬的解法中,这个题可以用本身的性质来做,这个BST就是左 < 根 < 右,所以利用递归来判断是否满足这个条件。可以设置最小值和最大值。
C++代码:
using ll = long long; // C++11里面的特性。
/**
* 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) {
return valid(root,LONG_MIN,LONG_MAX);
}
bool valid(TreeNode *root,ll mmin,ll mmax){
if(!root) return true;
if(root->val <= mmin || root->val >= mmax) return false;
return valid(root->left,mmin,root->val) && valid(root->right,root->val,mmax);
}
};
(BST 递归) leetcode98. Validate Binary Search Tree的更多相关文章
- leetcode98 Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- 【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) ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
随机推荐
- javascript排序算法-归并排序
归并排序 概念:归并排序是一种分治算法.其思想是将原始数组切分成较小的数组,直到每个小数组只有一个位置,接着将小数组归并成较大的数组,直到最后只有一个排序完毕的大数组. 时间复杂度: O(nlogn) ...
- adb 查看 android手机的CPU架构
adb shell cat /proc/cpuinfo 当然要下载adb并配置好环境变量
- wxpython 支持python语法高亮的自定义文本框控件的代码
在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...
- JMeter接口测试实战-动态数据验证
JMeter接口测试实战-动态数据验证 说到验证就不得不说断言, 先来看下JMeter官方给出断言(Assertion)的定义, 用于检查测试中得到的响应数据等是否符合预期,用以保证测试过程中的数据交 ...
- 在xp下无人值守自动安装系统
无人值守安装可以大大缩短安装系统的时间.我在虚拟机测试成功. 先给文件链接https://files.cnblogs.com/files/sishenzaixian/%E8%87%AA%E5%8A%A ...
- 使用Navicat快速生成MySQL数据字典
1.通过information_schema.COLUMNS表 查询该表可得到所需字段信息 SELECT * FROM information_schema.COLUMNS; 如下图所示: 2.示例 ...
- Mysql 字符串指定位置插入空格
UPDATE flow_data_243 SET data_15=CONCAT(LEFT(data_15,10),' ',RIGHT(data_15,LENGTH(data_15)-10)) WHER ...
- 看AppCan移动管理平台如何助力企业移动化
AppCan企业移动管理平台(EMM)是为企业移动化战略提供综合管理的平台产品.AppCan EM移动管理平台为企业提供对用户.应用.设备.内容.邮件的综合管理服务,并在此基础上为企业提供统一应用商店 ...
- ASP.NET Core 2.1 : 十四.静态文件与访问授权、防盗链
我的网站的图片不想被公开浏览.下载.盗链怎么办?本文主要通过解读一下ASP.NET Core对于静态文件的处理方式的相关源码,来看一下为什么是wwwroot文件夹,如何修改或新增一个静态文件夹,为什么 ...
- 单列集合类的根接口Collection
Collection接口 Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合.JDK 不提供此接 ...