in JavaScript
Think of linked lists but with children instead of siblings
In order for a set of nodes and children to be considered a tree it must satisfy some properties:
Hierarchical vs Linear datastructure.
Depth first and breadth first are not the only ones available
But they are given in interviews sometimes
how do we go about searching?
DFS
parameters node, isMatch callback
if node is a match, return true
if there is a left node
if DFS(left node, isMatch)
return true
if there is a right node
if DFS(right node, isMatch)
return true
no more children, return false
answer the following question
how do we go about searching?
start breadth first search
parameters root node, isMatch callback
create a queue
add the root node to the queue
start forever loop
if queue is empty, return false
get node from queue and remove it from queue
if the node matches, return true
add the nodes children to the queue
return false