Validate Binary Search Tree
MediumGiven the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST must satisfy the following conditions
- The left subtree of a node contains only nodes with values less than the node's value.
- The right subtree of a node contains only nodes with values greater than the node's value.
- Both left and right subtrees must also be binary search trees.
Example:
Input: root = [2,1,3] Output: true Explanation: The tree follows BST rules. Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The node with value 4 has a left child with value 3, violating the BST property.
Test Cases
Test Cases
Input
6 4 3 x x 5 x x 8 x x
Expected Output
true
Step 1
Step 2
Step 3
Step 1: Identify the Pattern
Validate Binary Search Tree
MediumGiven the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST must satisfy the following conditions
- The left subtree of a node contains only nodes with values less than the node's value.
- The right subtree of a node contains only nodes with values greater than the node's value.
- Both left and right subtrees must also be binary search trees.
Example:
Input: root = [2,1,3] Output: true Explanation: The tree follows BST rules. Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The node with value 4 has a left child with value 3, violating the BST property.
Test Cases
Test Cases
Input
6 4 3 x x 5 x x 8 x x
Expected Output
true
Step 1
Step 2
Step 3
Step 1: Identify the Pattern