Here is the interview question prompt, presented for reference.
Given a binary search tree
like the one below, can you write a function that will return true
if it is valid?
5
/ \
3 9
/ \
1 4
Recall that a BST
is valid only given the following conditions:
- The left child subtree of a node contains only nodes with values less than the parent node's.
- The right child subtree of a node contains only nodes with values greater than the parent node's.
- Both the left and right subtrees must also be BSTs.
You can assume that all nodes, including the root, fall under this node definition:
class Node {
constructor(val) {
this.value = val;
this.left = null;
this.right = null;
}
}
The method may be called like the following:
root = new Node(5);
root.left = new Node(3);
root.right = new Node(9);
console.log(isValidBST(root));
100000
-1000000000
and 1000000000
O(n)
O(logn)
or O(d)
where d
is the depth of the treeYou can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Validate a BST.