Validate Binary Search Tree Leetcode Recursion 3 line solution

preview_player
Показать описание
Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid 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.
Рекомендации по теме
Комментарии
Автор

super explanation i was stuck on it for long

himcooljig
Автор

Great explanation i was just stuck with this weird overflow error,
Thanks for the explanation

AmanRaj-gyqv
Автор

Ma'am what will be the value of Max in right subtree call...

Secret_Superstar_Girl
Автор

As new test cases are added change the INT_MIN and INT_MAX
class Solution {
public:
bool checkValidBst(TreeNode* root, long long min, long long max){

if(root==NULL) return true;

if(root->val<=min || root->val>=max)
{
return false;
}
return checkValidBst(root->left, min, root->val) && checkValidBst(root->right, root->val, max);

}
bool isValidBST(TreeNode* root) {
if(root->left==NULL && root->right==NULL) return true;
return checkValidBst(root, LLONG_MIN, LLONG_MAX);
}
};

aiyappamn