Binary trees & traversal

Hierarchical data branching into at most two children per node.

The idea

A Binary Tree is a set of nodes where each node has a left and right pointer. Because it branches, we can't just loop from 0 to N. We have to traverse it.

Depth-First Search (DFS) dives all the way down to a leaf before coming back up. It uses recursion (or a Stack) and comes in 3 flavors: Pre-order (visit node before children), In-order (visit node between children), and Post-order (visit node after children).

Breadth-First Search (BFS) explores level by level using a Queue.

Output: []

How it works

def dfs_in_order(node):
    if not node:
        return
        
    dfs_in_order(node.left)
    print(node.val)         # Visit logic happens HERE
    dfs_in_order(node.right)