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 ...
随机推荐
- PHP SSH2 不支持 IdentityFile
有的情况下 我们会用到 类似命令行 sftp -o IdentityFile=.ssh/identity username@host方式 登陆, 想用php 操作, 但是 php 现在看是不支持的, ...
- mysql5.7在线更改innodb_buffer_pool_size
show variables like 'innodb_buffer_pool_size'; set global innodb_buffer_pool_size=;# 在线更改该值时,不会立即生效, ...
- 廖雪峰Java10加密与安全-3摘要算法-5Hmac
1 比较MD5和HamcMD5 HmacMD5可以看作带安全salt的MD5 import javax.crypto.KeyGenerator; import javax.crypto.Mac; im ...
- 同名的cookie会不会存在多个
cookie new了多个.同一个名字.会不会存在多个呢. //若果不设置Cookie的path,则名字相同的Cookie视为相同的Cookie,后面的覆盖前面的,注意:大小写敏感 Cookie c1 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子[转]
一.一个叫声接口和几只鸭子 从一个叫声接口开始. {<HeadFirst设计模式>Delphi代码之模式小结 } { 一个叫声接口 } ...
- PAT甲级——A1063 Set Similarity
Given two sets of integers, the similarity of the sets is defined to be /, where Nc is the number ...
- PAT甲级——A1032 Sharing
To store English words, one method is to use linked lists and store a word letter by letter. To save ...
- Maven实战04_使用Archetype生成项目骨架
在上一章中的HelloWorld中,我们的项目遵循了一些Maven项目的约定 在项目的根目录中放置pom.xml 在src/main/java目录中放置项目的主代码 在src/test/java目录中 ...
- 10 种最常见的 Javascript 错误(频率最高)
本文是小编给大家收藏的JavaScript 中频度最高的 10 种错误,我们会告诉你什么原因导致了这些错误,以及如何防止这些错误发生.写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学 ...
- JobTracker与TaskTracker的关系
JobTracker 对应于 NameNode TaskTracker 对应于 DataNode DataNode 和NameNode 是针对数据存放来而言的 JobTracker和TaskTrac ...