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的更多相关文章

  1. leetcode98 Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  2. Leetcode98. Validate Binary Search Tree验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

  3. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. 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 ...

随机推荐

  1. 小tips:使用rem+vw实现简单的移动端适配

    首先设置meta属性,如下代码: <meta name="viewport" content="width=device-width, initial-scale= ...

  2. 一起学Android之Intent

    本文简述在Android开发中Intent的常见应用,仅供学习分享使用. 什么是Intent? Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Inten ...

  3. 章节十、6-CSS---用CSS 定位子节点

    以该网址为例(https://learn.letskodeit.com/p/practice) 一.通过子节点定位元素 1.例如我们需要定位这个table表格 2.当我们通过table标签直接定位时, ...

  4. DataPipeline创始人&CEO 陈诚:沃森与AI

    引言:本文来自infoQ架构师电子月刊对DataPipeline创始人&CEO陈诚的约稿.陈诚,毕业于上海交大,留学于美国密西根大学,前Yelp大数据研发工程师,曾就职于美国Google.Ye ...

  5. 简单的C#实体映射 AutoMapper

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 要映射实体 public class SourceModel { public int ...

  6. Maven settings.xml

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  7. mybatis常见错误

    1.传入单个参数为list时 List<Objects> query(@param("list") List<String> list) <selec ...

  8. pyhton从开始到光棍的day11

    纪念我这个开始学python的光棍天,光棍天依旧是函数,这次的函数有个小高级,比昨天的low函数稍微高级点,就是在使用函数中是可以赋值的,比如索引一个值什么的.函数还可以当做参数进行传递,把这个函数名 ...

  9. android获取string.xml的值

    在android开发过程中,编写java代码中的常量过一般情况下,我们是定义在string.xml这个文件中.这样修改起来也很方便,而且做国际化也很简单. 这个string.xml的值会被R文件映射, ...

  10. 毕业设计(1)基于MicroPython的大棚监测控制系统的程序设计与模型设计

    智慧农业就是将物联网技术运用到传统农业中去,运用传感器和软件通过移动平台或者电脑平台对农业生产进行控制,使传统农业更具有“智慧”.除了精准感知.控制与决策管理外,从广泛意义上讲,智慧农业还包括农业电子 ...