(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 ...
随机推荐
- ES6基础(二)
一.ES6字符串扩展 字符串模板 在传统的JavaScript语言中,输出模板通常是这样写的. 上面这种写法繁琐不方便,于是ES6中引入了字符串模板解决这个问题. 用反引号(`)标识.他可以当做普通字 ...
- 报错TypeError: $(...).live is not a function解决方法
报错的原因是这个方法在jquery1.7以后就被废除了, 1.7以后的版本改用.on()方法 之前的用法: .live(events, function) 新方法: .on(eventType, se ...
- web安全基础第一天
编码:url编码 base64编码:末尾有俩个==号 md5加密:16位或者32位 1.whois查询 :站长邮箱,联系人,备案 旁站c段&子域名. ( 大数据平台和bing接口查询 :Zo ...
- jquery字符串操作
目的:把自己常用到的jquery操作字符串总结一下 w3cSchool关于js字符串的整理:有需要的时候可以查 http://www.w3school.com.cn/js/jsref_obj_stri ...
- CentOS7.x搭建时间同步服务器
关于chrony Chrony是一个开源的自由软件,像CentOS7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP) ...
- 【憩园】C#并发编程之异步编程(一)
写在前面 C#5.0中,对异步编程进行了一次革命性的重构,引入了async和await这两个关键字,使得开发人员在不需要深刻了解异步编程的底层原理,就可以写出十分优美而又代码量极少的代码.如果使用得当 ...
- 随心测试_软测基础_008<测试对象整体认识>
关于:软件测试的整体认识,首先:认识:测试 对象 与 测试主体(人) 之间的关系 核心理解如下: 不同的测试对象,选取 不同的测试方法 软件项目的周期中:提出 需求 ——>软件实现—— ...
- jeecg入门操作—模板配置(录入界面)
点击online表单的模板配置,进入模板设计列表页面,点击创建模板 点击创建模板 点击激活 设计完成,点击激活(表单模板可以多个,激活状态只能有一个) 激活后,重新加入功能测试,点击添加页面,效果如下 ...
- 用div画三角/矩形/圆
1. 画三角 <!DOCTYPE html> <html> <head> <title></title> <meta charset= ...
- 分布式存储ceph——(1)部署ceph
前言: 很多朋友想学ceph,但是开始ceph部署就让初学者举步为艰,ceph部署时由于国外源的问题(具体大家应该懂得),下载和安装软件便会卡住,停止不前.即使配置搭建了国内源后,执行ceph-dep ...