Search in a binary search tree

LeetCode problem 700. Search in a binary search tree.

## Problem description

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`.

Example 1

**Input**: root = [4,2,7,1,3], val = 2
**Output**: [2,1,3]
Example 2

**Input**: root = [4,2,7,1,3], val = 5
**Output**: []
Constraints
* The number of nodes in the tree is in the range `[1, 5000]`.
* `1 <= Node.val <= 107`
* `root` is a binary search tree.
* `1 <= val <= 107`

## Analysis

Here are some important properties of a binary search tree:

1. There may NOT be duplicate keys in a binary search tree.
2. For *every node*, the keys of all nodes in this node's *left sub-tree* are *strictly less than* the key of this node.
3. For *every node*, the keys of all nodes in this node's *right sub-tree* are *strictly greater than* the key of this node.

Based on these properties, we start from the root, and compare the key of the current node and the desired key.

* If current node's key > desired key, go into left subtree.
* If current node's key < desired key, go into right subtree.
* If current node's key = desired key, return current node.

Repeat until we reach the bottom.

If we have reached the bottom without finding the desired key, return `null`.

### Complexity analysis

#### Time complexity

Note that in the worst case the time complexity is **O(n)** when the tree is not necessarily balanced.

**Example**: If we have a tree like

```
      1
       \
        2
         \
          3
           \
            4

```
and we want to find 4, then we have to traverse the entire tree to obtain it.

#### Space complexity

Stack
We just need to use a pointer to traverse the tree from root to bottom. The space complexity on stack is O(1).
Heap
We did not allocate any new objects. The space complexity on heap is O(1).
So the total space complexity is **O(1)**.

## Solution

### Iterative solution

#### C++

```
/\*\*
 \* Definition for a binary tree node.
 \* struct TreeNode {
 \* int val;
 \* TreeNode \*left;
 \* TreeNode \*right;
 \* TreeNode() : val(0), left(nullptr), right(nullptr) {}
 \* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 \* TreeNode(int x, TreeNode \*left, TreeNode \*right) : val(x), left(left), right(right) {}
 \* };
 \*/
class Solution {
public:
 TreeNode\* searchBST(TreeNode\* root, const int& val) {
 // iterative solution

 // corner case
 if (root == nullptr) {
 return root;
 }

 TreeNode\* curr = root;
 while ((curr != nullptr) && (curr->val != val)) {
 if (curr->val > val) {
 curr = curr->left;
 }
 else {
 curr = curr->right;
 }
 }
 return curr;
 }
};

```

#### Java

```
/\*\*
 \* Definition for a binary tree node.
 \* public class TreeNode {
 \* int val;
 \* TreeNode left;
 \* TreeNode right;
 \* TreeNode() {}
 \* TreeNode(int val) { this.val = val; }
 \* TreeNode(int val, TreeNode left, TreeNode right) {
 \* this.val = val;
 \* this.left = left;
 \* this.right = right;
 \* }
 \* }
 \*/
class Solution {
 public TreeNode searchBST(TreeNode root, int val) {
 // iterative solution

 // corner case
 if (root == null) {
 return root;
 }

 TreeNode curr = root;
 while ((curr != null) && (curr.val != val)) {
 if (curr.val > val) {
 curr = curr.left;
 }
 else {
 curr = curr.right;
 }
 }
 return curr;
 }
}

```

### Recursive solution

#### C++

```
/\*\*
 \* Definition for a binary tree node.
 \* struct TreeNode {
 \* int val;
 \* TreeNode \*left;
 \* TreeNode \*right;
 \* TreeNode() : val(0), left(nullptr), right(nullptr) {}
 \* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 \* TreeNode(int x, TreeNode \*left, TreeNode \*right) : val(x), left(left), right(right) {}
 \* };
 \*/
class Solution {
public:
 TreeNode\* searchBST(TreeNode\* root, const int& val) {
 // recursive solution

 // corner case
 if (root == nullptr) {
 return root;
 }

 if ((root->left != nullptr) && (root->val > val)) {
 return searchBST(root->left, val);
 }
 else if ((root->right != nullptr) && (root->val < val)) {
 return searchBST(root->right, val);
 }
 else {
 return (root->val == val) ? root : nullptr;
 }
 }
};

```

#### Java

```
/\*\*
 \* Definition for a binary tree node.
 \* public class TreeNode {
 \* int val;
 \* TreeNode left;
 \* TreeNode right;
 \* TreeNode() {}
 \* TreeNode(int val) { this.val = val; }
 \* TreeNode(int val, TreeNode left, TreeNode right) {
 \* this.val = val;
 \* this.left = left;
 \* this.right = right;
 \* }
 \* }
 \*/
class Solution {
 public TreeNode searchBST(TreeNode root, int val) {
 // recursive solution

 // corner case
 if (root == null) {
 return root;
 }

 if ((root.left != null) && (root.val > val)) {
 return searchBST(root.left, val);
 }
 else if ((root.right != null) && (root.val < val)) {
 return searchBST(root.right, val);
 }
 else {
 return (root.val == val) ? root : null;
 }
 }
}

```

## See also

* Insert into a binary search tree
* Delete node in a binary search tree