← 返回 amazon 的题目列表Validate Binary Search Tree
类型:online_judge
Problem: Validate Binary Search Tree
Given the root of a binary tree root, determine whether 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 values strictly less than the node's value.
The right subtree of a node contains only nodes with values strictly greater than the node's value.
Both the left and right subtrees must also be valid BSTs.
Note: duplicate values are not allowed in this BST definition.
Input Format
The input is a level-order representation of a binary tree, for example:
[2,1,3]
null represents an empty node.
Output Format
Print:
true
if the tree is a valid BST; otherwise print:
false
Constraints
Number of nodes: 0 <= n <= 10^4
Node value range: -2^31 <= Node.val <= 2^31 - 1
Example 1
Input:
[2,1,3]
Output:
true
Example 2
Input:
[5,1,4,null,null,3,6]
Output:
false
Explanation: The root value is 5, but the right subtree contains node 3, which is less than 5, so the tree is not a valid BST.
Example
Input
[2,1,3]
Output
true