FastPrepValidate a Strict Binary Search Tree

Validate a Strict Binary Search Tree

Amazon logoAmazon● MediumINTERNPHONE SCREEN
Learn

Problem statement

Determine whether a binary tree is a binary search tree.

For this exercise, assume a strict BST: every value in a node's entire left subtree is smaller than that node's value, and every value in its entire right subtree is larger. Equal values are not allowed. An empty tree is valid.

Function

isStrictBST(root: TreeNode) → boolean

Examples

Example 1

root = [2,1,3]return = true

All left-subtree values are smaller and all right-subtree values are larger.

Example 2

root = [5,1,4,null,null,3,6]return = false

The right subtree contains 4 and 3, which violate the root bound 5.

Example 3

root = []return = true

For this exercise, the empty tree is valid.

Constraints

  • For this exercise, assume a finite acyclic binary tree containing 0 through 500 nodes.
  • -10^9 <= node.val <= 10^9.

More Amazon problems

See Amazon hiring insights
public boolean isStrictBST(TreeNode root) {
    // Write your code here
}
root[2,1,3]
expectedtrue
Checking account…