← 返回 goldmansachs 的题目列表Validate a Binary Search Tree
类型:online_judge
Write pseudocode or code to determine whether a binary tree is a valid Binary Search Tree (BST).
A valid BST requires every value in a node's left subtree to be strictly smaller than the node, and every value in its right subtree to be strictly larger.
The input is a level-order array representation of a binary tree, where null represents a missing node. Output true or false.
Examples
Input: [2,1,3]
Output: true
Input: [5,1,4,null,null,3,6]
Output: false
Node values may be any 32-bit integers. Comparing a node only with its direct children is insufficient.
Example
Input
2 1 3
Output
true