
g0001_0100.s0098_validate_binary_search_tree.Solution.c Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
// #Binary_Search_Tree #Data_Structure_I_Day_14_Tree #Level_1_Day_8_Binary_Search_Tree
// #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(log(N))
// #2024_11_02_Time_0_ms_(100.00%)_Space_11.9_MB_(57.64%)
#include
#include
#include
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
// Helper function to check if the tree is a valid BST
bool solve(struct TreeNode* root, long left, long right) {
if (root == NULL) {
return true;
}
if (root->val <= left || root->val >= right) {
return false;
}
return solve(root->left, left, root->val) && solve(root->right, root->val, right);
}
// Main function to check if a tree is a valid BST
bool isValidBST(struct TreeNode* root) {
return solve(root, LONG_MIN, LONG_MAX);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy