LeetCode --- Validate Binary Search Tree
判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST
如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况)
附上代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// "fa" holds the last value that has been visited
// "flag" is false when it(the given binary tree or its subtree) is an invalid BST
void InOrder(TreeNode *root, int& fa, bool& flag) {
if (root->left != NULL) {
InOrder(root->left, fa, flag);
}
if (root->val <= fa)
flag = false;
fa = root->val;
if (root->right != NULL) {
InOrder(root->right, fa, flag);
}
}
bool isValidBST(TreeNode *root) {
if (root == NULL || root->left==NULL&&root->right==NULL) return true;
// initialize "fa" as INT_MIN
// I assume that there are no tree node's val equals to INT_MIN
// and it does... (test case like this doesnt exist)
int fa = INT_MIN;
bool flag = true;
InOrder(root, fa, flag);
if (flag)
return true;
else
return false;
}
};
LeetCode --- Validate Binary Search Tree的更多相关文章
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- Java for LeetCode 098 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【题解】【BST】【Leetcode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- 如何通过EditPlus远程连接Linux
1. File - FTP - FTP Settings 2. Add 3. 填写Linux的ip地址及用户名和密码 4. OK
- pyinstaller 打包python3.6文件成exe 运行
1.安装pyinstaller 切换到安装目录下script 运行 如我的目录:F:\Program Files\Python36\Scripts pip install pyinstaller ...
- 不同目录cookie共享的问题解决 cookie不同页面访问不到的问题
一般设置cookie的方法是setcookie(key, value, expire),参数分别的意思是建.值.过期时间,这里是大众的默认设置方法,但是忽略了一个问题,setcookie还有path与 ...
- JDBC中DAO+service设计思想
一.DAO设计思想 a) Data access Object(数据访问对象):前人总结出的一种固定模式的设计思想. 高可读性. 高复用性. 高扩展性. b) JDBC代码实现的增删改查操作是有复用需 ...
- PHP 缓存详解
为什么要使用缓存 一个网站或者一个应用的标准流程是浏览器向应用服务器发出请求,应用服务器做一些计算和逻辑判断之后再请求数据库,数据库收到请求后在经过计算将数据返回给应用服务器,应用服务器再次计算后把数 ...
- webServices学习二(小试牛刀。jdk 方式发布一个应用)
一.前提 1.用Jdk1.6.0_21以后的版本发布一个WebService服务. 2.与Web服务相关的类,都位于javax.jws.*包中. 1.主要类有: 1.@WebService - 它是 ...
- HDFS 冗余数据保存
- 在PyCharm中导入Numpy和Pygame模块 (win8.1)
我用的是anaconda安装python3.6 已经在终端 pip install numpy 但是在pycharm运行程序出现错误:ImportError: No module named nump ...
- ECS应用管理最佳实践
前言 即使在CloudNative发展如火如荼的当下,ECS应用(直接将应用部署在ECS上,不使用容器)仍然占了相当大的比重,原因主要在于相对容器化应用,ECS应用由于不需要容器的运行时环境和类似K8 ...
- Qt运行不出现界面
安装Qt之后按照例程运行,结果不出现界面,原因是路径中有中文,将中文全部改成英文之后,问题解决.