Is This Binary Tree a BST
Updated
Problem statement
Given the root of a binary tree, return true if it is a binary search tree, and false otherwise.
A binary tree is a binary search tree when, for every node, every value in its left subtree is strictly smaller than the node's value and every value in its right subtree is strictly larger. Checking only a node's two children is not enough: the rule covers the whole subtree. A value that appears twice makes the answer false.
Function
isValidBst(root: TreeNode) → booleanExamples
Example 1
root = [8,4,12,2,6,10,14]return = trueEvery node is larger than everything on its left and smaller than everything on its right (for example, the root 8 has 2, 4, 6 on its left and 10, 12, 14 on its right), so the answer is true.
Example 2
root = [8,4,12,2,9]return = false9 is the right child of 4, which is fine for 4, but 9 sits in the left subtree of the root 8 and is larger than 8. So the answer is false.
Example 3
root = [7,7]return = falseThe left child has the same value as the root. Values on the left must be strictly smaller, so the answer is false.
Constraints
- The tree has
1to500nodes. -2^31 <= Node.val <= 2^31 - 1
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Inorder Predecessor in a BSTNutanix · PHONE SCREENNutanix interview questions
- Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
- Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
- Check the Order of a Binary Search TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)