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 ...
随机推荐
- [转]Visual Studio 2010单元测试(1)--运行和定义普通单元测试
Visual Studio 2010 运行和定义单元测试 在VS2010中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 19-10-26-F
ZJ一下: T1码了暴力但是并没有开出来身高的神奇性质…… T2打模拟,但是只摸了卅分 T3不会,码了一个测试点分治.10分 TJ一下: T1. 发现身高范围在$[140,200]$时,直接去重跑$\ ...
- 解决maven项目创建过慢的问题
关键就在这里:添加以下键值对 archetypeCataloginternal 之后点击Next,再单击Finish即可.
- 【BZOJ4161】Shlw loves matrixI
题目描述 给定数列 {hn}前k项,其后每一项满足 hn = a1h(n-1) + a2h(n-2) + ... + ak*h(n-k) 其中 a1,a2...ak 为给定数列.请计算 h(n),并将 ...
- springmvc 串口读写 基于win7使用txrx netbeans jdk1.8 maven的
引入 <dependency> <groupId>org.rxtx</groupId> <artifactId>rxtx</artifactId& ...
- mysql数据库引擎(转载)
数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...
- 016-WebDriver API(2)
1. 多表单切换 WebDriver只能在一个页面上对元素进行识别和定位,无法直接定位frame/iframe表单内嵌页面上的元素,这是就需要通过switch_to.frame()方法将当前定位的主体 ...
- 一、WebService基础概念
一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intrane ...
- HDU 3086 马拉车模板
模板,但是对这个算法还是不太清楚,真实不明觉厉.... #include <iostream> #include <cstdio> #include <string.h& ...