题目:

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.
  • A single node tree is a BST

An example:

  2
/ \
1 4
/ \
3 5

The above binary tree is serialized as {2,1,4,#,#,3,5} (in level order).

题解:

  按照定义,二叉搜索树的中序遍历是升序序列

Solution 1 ()

class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
bool isValidBST(TreeNode *root) {
TreeNode *prev = nullptr;
return validate(root, prev);
} bool validate(TreeNode *node, TreeNode* &prev) {
if (node == nullptr) {
return true;
}
if (!validate(node->left,prev)) {
return false;
}
if (prev != nullptr && prev->val >= node->val) {
return false;
}
prev = node; return validate(node->right, prev);
}
};

  分治法 from here

Solution 2 ()

class ResultType {
public:
bool isBST;
TreeNode *maxNode, *minNode;
ResultType(): isBST(true), maxNode(nullptr), minNode(nullptr) {}
};
class Solution {
public: bool isValidBST(TreeNode *root) {
ResultType result = helper(root);
return result.isBST;
} ResultType helper(TreeNode *root) {
ResultType result;
if (root == nullptr) {
return result;
} ResultType left = helper(root->left);
ResultType right = helper(root->right); if (!left.isBST || !right.isBST) {
result.isBST = false;
return result;
}
if (left.maxNode != nullptr && left.maxNode->val >= root->val) {
result.isBST = false;
return result;
}
if (right.minNode != nullptr && right.minNode->val <= root->val) {
result.isBST = false;
return result;
} result.isBST = true;
result.minNode = left.minNode == nullptr ? root : left.minNode;
result.maxNode = right.maxNode == nullptr ? root : right.maxNode; return result;
}
};

【Lintcode】095.Validate Binary Search Tree的更多相关文章

  1. 【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) ...

  2. 【LeetCode】98. Validate Binary Search Tree

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

  3. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  4. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  5. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. 【LeetCode】99. Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  8. 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal

    题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...

  9. 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

随机推荐

  1. 【转】AngularJs 弹出框 model(模态框)

    原文转至 http://blog.csdn.net/violet_day/article/details/17170585 $modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 ...

  2. Redis闲谈(1):构建知识图谱

    场景:Redis面试 (图片来源于网络) 面试官: 我看到你的简历上说你熟练使用Redis,那么你讲一下Redis是干嘛用的? 小明: (心中窃喜,Redis不就是缓存吗?)Redis主要用作缓存,通 ...

  3. js 显示当前系统时间

    <div id="test"></div>                <script >                    setInt ...

  4. 三、Silverlight中使用MVVM(三)——进阶

    这篇主要引申出Command结合MVVM模式在应用程序中的使用 我们要做出的效果是这样的 就是提供了一个简单的查询功能将结果绑定到DataGrid中,在前面的基础上,这个部分相对比较容易实现了 我们在 ...

  5. 结缘mac

    还记得上一次买MacBookPro.是在去年的7月下旬,记得那次是我大学第一次买电脑,那时候刚准备開始研究android.听stormzhang以及android开发界的大佬们对mac开发androi ...

  6. Ubuntu/CentOS下编译Nginx最基本参数

    Ubuntu/CentOS下编译Nginx安装基本参数,做个记录: groupadd www useradd -g www www ./configure --user=www --group=www ...

  7. 【Scala】Scala的Predef对象

    隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...

  8. C#之stream

    在C#中经常要用stream stream下面主要有 FileStream:使用文件作为后备设备. BufferedStream:使用缓冲区作为后备设备,用来增强性能的中间存储. MemoryStre ...

  9. 3.11 T-SQL语句

    T-SQL语句 1.创建表create table Car     --创建一个名字是Car的表-- ( Code varchar(50) primary key, --第一列名字是Code 数据类型 ...

  10. 网页直播、微信直播技术解决方案:EasyNVR与EasyDSS流媒体服务器组合之区分不同场景下的easynvr

    近期遇到好多客户咨询关于实现微信直播.或者是将直播页面集成进入自己项目中. 该方案的主要目的:完成在公网一直进行内网摄像头的RTMP/HLS直播! 实现方案的具体实现: EasyNVR+EasyDSS ...