Practice/Amazon/Leetcode 98. Validate Binary Search Tree
CodingOptional
Given the root of a binary tree, determine whether it represents a valid binary search tree (BST).
A valid BST is defined as follows:
Note that these conditions must hold globally across the entire tree, not just for immediate children. For example, all nodes in the left subtree must be less than the root, not just the immediate left child.
true if the tree is a valid BST, false otherwise[0, 10^4]-2^31 <= Node.val <= 2^31 - 1Example 1:
Input: root = [5, 3, 8, 1, 4, null, 9] 5 / \ 3 8 / \ \ 1 4 9 Output: true Explanation: Every node satisfies the BST property. All left descendants are smaller and all right descendants are larger.
Example 2:
Input: root = [5, 1, 6, null, null, 4, 7] 5 / \ 1 6 / \ 4 7 Output: false Explanation: Node 4 is in the right subtree of root 5, but 4 < 5, violating the BST property.
Example 3:
Input: root = [2, 2, 3] 2 / \ 2 3 Output: false Explanation: The left child has value 2, which equals the parent. BST requires strict inequality.