Validate Binary Search Tree
Updated
Problem statement
Given the root of a binary tree, return whether it is a valid binary search tree.
For every node, every value in its left subtree must be strictly smaller than the node's value, every value in its right subtree must be strictly greater, and both subtrees must satisfy the same rule. Duplicate values therefore make the tree invalid.
Function
isValidBST(root: TreeNode) → booleanExamples
Example 1
root = [2,1,3]return = trueThe left child is smaller than the root and the right child is larger, so the tree is valid.
Example 2
root = [5,1,4,null,null,3,6]return = falseNode 4 is in the root's right subtree but is smaller than root value 5.
Constraints
- The tree contains between
1and10000nodes. -2147483648 <= Node.val <= 2147483647.
More Tokopedia interview questions
- Kth Largest Element in an ArrayONSITE INTERVIEW · Seen Jul 2021
- Online Stock SpanONSITE INTERVIEW · Seen Jul 2021
- Vertical Order Traversal of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Interleaving StringPHONE SCREEN · Seen Jul 2021
- Delete Node in a BSTPHONE SCREEN · Seen Jul 2021
- Rotate ImagePHONE SCREEN · Seen Jul 2021
Similar questions at other companies
- Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
- Check the Order of a Binary Search TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- Add All Larger Values in a BSTAdobe · PHONE SCREENAdobe interview questions
- Inorder Predecessor in a BSTNutanix · PHONE SCREENNutanix interview questions
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions