We can perform any tree traversal technique like in-order, pre-order, post-order etc and check if the element is present while each node is visited. But of all traversals, pre-order traversal is more efficient as we skip traversing all the nodes in the left and right subtree if the current node’s value is equal to the value we are looking for.
Search for a value Recursively
In the recursive approach, we do a pre-order traversal: If the current node is equal to the value we are looking for, we return the node. Otherwise we search for the element in left subtree and if it is still not found, we search in the right subtree.
Search for a value Iteratively
The iterative version is similar to the recursive function. But instead of using recursive function calls using system’s stack, we use stack data structure and iteratively search in pre-order traversal fashion.