Validate Binary Search Tree
Medium Trees AI round
root is the root of a binary tree, which may be empty.
The tree is a binary search tree when every node holds a value that is larger than every value in its left subtree and smaller than every value in its right subtree. Each node is held to this on its own, so a value that suits its parent can still break the rule for a node further up. Equal values break it too, so a binary search tree never holds the same value twice.
Return true when the tree is a binary search tree and false when it is not. An empty tree, and a tree of one node, are binary search trees.
Example 1
Example 2
9 sits under 15, on the right of 10, yet it is smaller than 10: every node suits its parent, but 9 breaks the rule for the root.
Example 3
Constraints
0 ≤ number of nodes ≤ 10⁵-2³¹ ≤ node.val ≤ 2³¹ − 1
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Trees
- Lowest Common Ancestor of a Binary TreeAI round · Medium
- Binary Tree Level Order TraversalAI round · Medium
- Binary Tree Vertical Order TraversalMedium
- Find Leaves of Binary TreeMedium
- Inorder Successor in BSTMedium