Validate Binary Search Tree
Validate Binary Search Tree
Problem Description
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
- The left subtree of a node contains only nodes with keys strictly less than the node’s key.
- The right subtree of a node contains only nodes with keys strictly greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
Intuition
Based on the BST definition, we could set min and max as the value’s boundary. The left subtree’s value must be strictly less than the root value, and vice versa for the right subtree (strictly greater).
Algorithm
1 | class Solution { |
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 YouChen's Blog!