【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 ...
随机推荐
- mybatis的拦截器及分页机制
https://blog.csdn.net/ssuperlg/article/details/79847889
- 关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题
关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题,有需要的朋友可以参考下. php调用java写的soap接口经验: 场景一: java是以数组的形式接收参数的 ...
- Python中urllib2总结
使用Python访问网页主要有三种方式: urllib, urllib2, httpliburllib比较简单,功能相对也比较弱,httplib简单强大,但好像不支持session1. 最简单的页面访 ...
- 利用.dSYM跟.app文件准确定位Crash位置
本文转载至 http://blog.csdn.net/lvxiangan/article/details/28102629 利用.dSYM和.app文件准确定位Crash位置首先,确保 ...
- rabbitmq 安装-单点
centos6.5 rabbitmq搭建 环境:centos6.5 192.168.9.41 安装rabbitmq需要先安装erlang.rabbitmq3.6版本需要erlang R16B03 ...
- 【BZOJ1146】[CTSC2008]网络管理Network 树状数组+DFS序+主席树
[BZOJ1146][CTSC2008]网络管理Network Description M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工 ...
- Linux就该这么学--了解Shell脚本
有人曾经将Shell形容是人与计算机硬件的“翻译官”,Shell作为用户与Linux系统通讯的媒介.自身也定义了各种变量和参数,并提供了诸如循环.分支等高级语言才有的控制结构特性.如何正确的使用这些功 ...
- java实现二叉树的构建以及3种遍历方法(转)
转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...
- Java分支循环结构
一.Java分支结构 1.if语句:一个 if 语句包含一个布尔表达式和一条或多条语句. if 语句的用语法如下: if(布尔表达式){ 如果布尔表达式为true将执行的语句 } public c ...
- [2018-11-27]2018年12月1日宁波dotnet社区线下活动
离上次活动,转眼又过了一个月,幸得各路大神支持,于本周六(12月1日),宁波dotnet社区的线下分享活动又来啦! 活动嘉宾及主题 董斌辉 2015-2019年微软全球最有价值专家(.NET方向) 2 ...