```
/**
* Definition for a binary tree node.
**/
class TreeNode
{
constructor(value, left, right)
{
this.value = value
this.left = left
this.right = right
}
}
/**
* You are given the root of a binary search tree (BST) and an integer val.
* Find the node in the BST that the node's value equals val and return the subtree rooted with that node.
* If such a node does not exist, return null.
*/
/**
* @param {TreeNode} root
* @param {int} val
* @return {Array}
**/
let searchBST = (root, val) => {
return searchValueBST(root, val)
};
let searchValueBST = (node, val, list = []) => {
if (!node) return list
if (node.value === val)
{
list.push(node.value)
if (node.left) list.push(node.left.value)
if (node.right) list.push(node.right.value)
return list
}
if (list.length > 0) return list
searchValueBST(node.left, val, list)
searchValueBST(node.right, val, list)
return list
};
```

# Algorithm: Search in a Binary Search Tree

Posted by

## Leave a Reply