BFS
Breadth First Search
- Queue
: go same level node first and go deeper
- push root node
- pop root node
- push children nodes From Left to Right
- pop child node one by one and…
- push children nodes from child (((repeat)))
1 | queue.enqueue(this); |
DFS
Depth First Search
- Stack
: go down to the leaf node first and move on to the other children
- push root node
- pop root node
- push children nodes From Left To Right
- pop child node and…
- push children nodes of child (((repeat)))
- pop the leaf node
- pop other leaf nodes on the same level / push children nodes of the node on the same level
- pop parent node and move on (((repeat)))
1 | stack.push(this); |