【Lintcode】095.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 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的更多相关文章
- 【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) ...
- 【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 ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【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 ...
- 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
随机推荐
- jsp获取web.xml 里的配置项
ServletContext servletContext = request.getSession().getServletContext(); String titl ...
- PLSQL怎样导出oracle表结构
tools->export tables 是导出表结构还有数据 tools->export user objects是导出表结构 可以用tools->export tables ...
- Oracle -- Create User
CREATE USER hibernate IDENTIFIED BY "123" DEFAULT TABLESPACE "HIBERNATE" TEMPORA ...
- BZOJ 1002 FJOI2007 轮状病毒 递推+高精度
题目大意:轮状病毒基定义如图.求有多少n轮状病毒 这个递推实在是不会--所以我选择了打表找规律 首先执行下面程序 #include<cstdio> #include<cstring& ...
- 初识python轻量web框架flask
1.使用pip安装Python包 大多数Python包都使用pip实用工具安装,使用pyvenv创建的虚拟环境会自动安装pip. 1.使用pip安装Flask(其它Python包同理) pip ins ...
- LA 3882 And Then There Was One[约瑟夫问题的变形]
And Then There Was One UVALive - 3882 Sample Input Sample Output //设f[i]为(原约瑟夫问题)第i次要删除的标号 #includ ...
- 【BZOJ1316】树上的询问 点分治+set
[BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...
- poj 2151Check the difficulty of problems<概率DP>
链接:http://poj.org/problem?id=2151 题意:一场比赛有 T 支队伍,共 M 道题, 给出每支队伍能解出各题的概率~ 求 :冠军至少做出 N 题且每队至少做出一题的概率~ ...
- 10分钟看懂, Java NIO 底层原理
目录 写在前面 1.1. Java IO读写原理 1.1.1. 内核缓冲与进程缓冲区 1.1.2. java IO读写的底层流程 1.2. 四种主要的IO模型 1.3. 同步阻塞IO(Blocking ...
- java 对象变量 c++对象指针 初始化对象变量的2中方法
java 对象变量 c++对象指针 java null引用 c++ null指针 Date deadline 是 对象变量,它可以引用Date类型的对象,但它不是一个对象,实际上它也没有引用对象. ...