Search in a binary search tree

problem [https://leetcode.com/problems/search-in-a-binary-search-tree/submissions/ 700. ].

Problem description
You are given the  of a binary search tree (BST) and an integer.

Find the node in the BST that the node's value equals  and return the subtree rooted with that node. If such a node does not exist, return.


 * Example 1
 * Search in a binary search tree 1.jpeg
 * Input: root = [4,2,7,1,3], val = 2
 * Output: [2,1,3]


 * Example 2
 * Search in a binary search tree 2.jpeg
 * Input: root = [4,2,7,1,3], val = 5
 * Output: []


 * Constraints
 * The number of nodes in the tree is in the range.
 * is a binary search tree.
 * is a binary search tree.

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

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