c_cpp 98.验证二进制搜索树
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 98.验证二进制搜索树相关的知识,希望对你有一定的参考价值。
//Runtime: 20 ms, faster than 99.86%
//Memory Usage: 20.7 MB, less than 37.44%
class Solution {
public:
bool isValidBST(TreeNode* root) {
return isValidBST(root,NULL,NULL);
}
bool isValidBST(TreeNode* root,TreeNode* minNode,TreeNode* maxNode){
if(!root) return true;
if(minNode && root->val <= minNode->val || maxNode && root->val >= maxNode->val)
return false;
return isValidBST(root->left,minNode,root) && isValidBST(root->right,root,maxNode);
}
};
//Runtime: 20 ms, faster than 99.86%
//Memory Usage: 20.8 MB, less than 21.53%
class Solution {
public:
bool isValidBST(TreeNode* root) {
TreeNode* prev = NULL;
return validate(root,prev);
}
bool validate(TreeNode* node,TreeNode* &prev){
if(node == NULL) return true;
if(!validate(node->left,prev)) return false;
if(prev != NULL && prev->val >= node->val) return false;
prev = node;
return validate(node->right,prev);
}
};
以上是关于c_cpp 98.验证二进制搜索树的主要内容,如果未能解决你的问题,请参考以下文章
java 98.验证二进制搜索树(#1递归).java
java 98.验证二进制搜索树(#1递归).java
java 98.验证二进制搜索树(#1递归).java
java 98.验证二进制搜索树(#1递归).java
java 98.验证二进制搜索树(#1递归).java
[JavaScript 刷题] 树 - 验证二叉搜索树, leetcode 98