All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0098_validate_binary_search_tree.Solution.cpp Maven / Gradle / Ivy

// #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_05_27_Time_3_ms_(97.92%)_Space_20.3_MB_(28.52%)

#include 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return solve(root, LONG_MIN, LONG_MAX);
    }

private:
    bool solve(TreeNode* root, long left, long right) {
        if (root == nullptr) {
            return true;
        }
        if (root->val <= left || root->val >= right) {
            return false;
        }
        return solve(root->left, left, root->val) && solve(root->right, root->val, right);
    }
};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy