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

g0001_0100.s0098_validate_binary_search_tree.Solution.swift 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_06_25_Time_14_ms_(93.62%)_Space_16_MB_(84.87%)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init() { self.val = 0; self.left = nil; self.right = nil; }
 *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
 *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
 *         self.val = val
 *         self.left = left
 *         self.right = right
 *     }
 * }
 */
class Solution {
    func isValidBST(_ root: TreeNode?) -> Bool {
        return solve(root, Int.min, Int.max)
    }

    private func solve(_ root: TreeNode?, _ left: Int, _ right: Int) -> Bool {
        guard let root = root else {
            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