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. 【20190220】HTTP-知识点整理:TCP/IP与HTTP

    TCP/IP是互联网相关的各类协议族的总称,HTTP属于它内部的一个子集. 一.TCP/TP的分层管理 1. 应用层 应用层决定了向用户提供应用服务时通信的活动.TCP/IP 协议族内预存了各类通用的 ...

  2. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  3. GZIP压缩与解压

    public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException ...

  4. 从零学习Fluter(六):Flutter仿boss直聘v1.0重构

    今天继续学习flutter,觉得这个优秀的东西,许多方面还需要完善,作为一个后来者,要多向别人学习.俗话说,“学无先后,达者为师”.今天呢,我又重新把flutter_boss这个项目代码 从头到脚看了 ...

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

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

  6. 关于executemany()方法在不同OS和DB API下的不同表现的测试

    昨天在参照着网上写一段关于MySQL连接池的配合gevent多线程调用的代码时遇到了一个问题,自己写的代码根本不能多线程执行,比单会话插入数据慢太多,直到今天早上才发现问题所在,把DB API从MyS ...

  7. RocketMQ4.3.X关于设置useEpollNativeSelector = true报错问题

    前一阵子刚整理完RocketMQ4.3.x版本的相关配置的工作,接下来就来测试一下改变参数会带来什么好的结果 首先我就选中了useEpollNativeSelector 这个参数 默认这个参数是 fa ...

  8. storm ui 网页一直出现提示loading summary

    在更换了一次storm的版本之后:访问 http://mini1:8080/index.html 来查看storm的运行情况,但是出现了网页一直出现提示loading summary,但是通过透明的弹 ...

  9. Linux 基础学习:文件权限与种类

    1.文件权限 linux系统中通过 “ls -al”,可查看当前目录的所有文件的详细信息. 第一列代表这个文件的类型与权限: 第一个字符表示文件类型: [d]:表示目录文件 [-]:表示普通文件 [l ...

  10. JProfiler的详细使用介绍

    转:https://blog.csdn.net/huangjin0507/article/details/52452946 下载软件 官网地址:http://www.ej-technologies.c ...