题目:

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. 解决Linux中文环境下astro和Calibre不能输入的问题

    例如我的opensuse在中文环境下不能在astro中输入指令,Calibre的grid spacing设置框不能输入,经过摸索,找到以下两种解决方法: 1.    将系统环境变成英文,在.bashr ...

  2. ck-reset css(2016/5/13)

    /**rest by 2016/05/04 */ * {box-sizing: border-box;} *:before,*:after {box-sizing: border-box;} body ...

  3. Bootstrap学习速查表(一) 理论基础

    参考网站http://www.bootcss.com/ 第一步,起步,引入基本样式 <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="st ...

  4. js关闭浏览器事件,js关闭浏览器提示及相关函数

    关于浏览器关闭事件的相关描述 有些朋友想在浏览器关闭的时候,弹出alert .confirm或者prompt等.实验证明,这种做法是失败的,原因是浏览器关闭事件自动屏蔽执行js的某些方法,从而防止恶意 ...

  5. Windows下安装Oracle 11g全过程。

    1. 解压Oracle11.1.0.6 for win32,然后点击setup 2.选择高级安装,下一步 3.选择企业版,下一步 4.修改Oracle基目录,也可以是默认,下一步 5.将状态复选框打上 ...

  6. 深入Asyncio(四)Coroutines

    Coroutines asyncio在3.4版本添加到Python中,但通过async def和await关键字创建coroutines的语法是3.5才加入的,在这之前,人们把generators当作 ...

  7. 09-redis事务及锁应用

    Redis 中的事务 Redis支持简单的事务 Redis与 mysql事务的对比 ------------------------------------------------------- My ...

  8. java 常用设计模式(转载)

    http://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html 设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得 ...

  9. eclipse tomcat maven

    jdk jre eclipse 略过 下载maven和tomcat 上apache官网下载maven:http://maven.apache.org/download.cgi. 上apache官网下载 ...

  10. CSDN专訪:大数据时代下的商业存储

    原文地址:http://www.csdn.net/article/2014-06-03/2820044-cloud-emc-hadoop 摘要:EMC公司作为全球信息存储及管理产品方面的率先公司,不久 ...