🤖
Data Structures and Algorithms
  • CS Theory And Problems
  • Big O
    • Big O Notation
  • Algorithms
    • Binary Search
    • Breadth First Search (BFS)
    • Depth First Search (DFS)
    • Dynamic Programming
    • Kadane's Algorithm
    • Kahn's Algorithm
    • Quickselect
    • Recursion
    • Sorting Algorithms
    • Sliding Window
  • Data Structures
    • Binary Heap
    • Graph
    • Linked List
    • Trees
  • Problems
    • LeetCode
      • 1. Two Sum
      • 3. Longest Substring Without Repeating Characters
      • 5. Longest Palindromic Substring
      • 11. Container With Most Water
      • 15. 3 Sum
      • 19. Remove Nth Node From End of List
      • 20. Valid Parentheses
      • 33. Search in Rotated Sorted Array
      • 49. Group Anagrams
      • 53. Maximum Subarray
      • 55. Jump Game
      • 56. Merge Intervals
      • 76. Minimum Window Substring
      • 98. Validate Binary Search Tree
      • 105. Construct Binary Tree from Preorder and Inorder Traversal
      • 121. Best Time to Buy and Sell Stock
      • 133. Clone Graph
      • 141. Linked List Cycle
      • 152. Maximum Product Subarray
      • 153. Find Minimum in Rotated Sorted Array
      • 200. Number of Islands
      • 206. Reverse Linked List
      • 207. Course Schedule
      • 217. Contains Duplicate
      • 226. Invert Binary Tree
      • 238. Product of Array Except Self
      • 242. Valid Anagram
      • 297. Serialize and Deserialize Binary Tree
      • 347. Top K Frequent Elements
      • 417. Pacific Atlantic Water Flow
      • 424. Longest Repeating Character Replacement
      • 435. Non-overlapping Intervals
      • 647. Palindromic Substrings
Powered by GitBook
On this page
  • Description
  • Solution
  1. Problems
  2. LeetCode

98. Validate Binary Search Tree

Previous76. Minimum Window SubstringNext105. Construct Binary Tree from Preorder and Inorder Traversal

Last updated 3 years ago

Description

See:

Note that in this version of a binary search trees, that duplicates are not allowed. The problem defines a binary search tree as:

  • 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.

Solution

This problem and its solution is also in Cracking the Coding Interview book.

Suppose we have the following tree:

Atribution: Pat Hawks, CC BY-SA 4.0 , via Wikimedia Commons

The idea to solve this problem is to think of a node as being between two values.

For the root node, 12, since it does not have a parent node, its value can be anything between negative infinity and infinity.

For the root's left child, its value must be between negative infinity and 12. In this case it is 6, so it is valid:

For the root's right child, its value must be between 12 and infinity. In this case it is 16, so it is valid:

Moving down the tree, we use the same formula for each node. So for node 6's left child its value must be between negative infinity and 6. For node 6's right child its value must be between 6 and 12.

And so on.

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def is_valid(node, left_val, right_val) -> bool:
            if node is None:
                return True
            if not(left_val < node.val < right_val):
                return False

            return is_valid(node.left, left_val, node.val) and is_valid(node.right, node.val, right_val)

        return is_valid(root, float("-INF"), float("INF"))
leftchild=−∞<6<12 left child = -\infty < 6 < 12leftchild=−∞<6<12
rightchild=12<16<∞right child = 12 < 16 < \inftyrightchild=12<16<∞
https://leetcode.com/problems/validate-binary-search-tree/
https://creativecommons.org/licenses/by-sa/4.0