Tree Patterns
Tree patterns: solving binary tree problems by passing values down or returning them up, level by level, or through both subtrees at once.
Tree patterns decide what each recursive call on a binary tree is handed by its parent, what it hands back, and what it records on the way, so one visit of every node answers the question. It matters because a tree question solved node by node, with a fresh search below each one, walks the lower nodes again and again, and because a tree whose levels are the answer needs a queue rather than recursion. The topic opens on depth passed down and height returned up on one tree, finds the longest path in a tree in one pass instead of measuring at every node, and gives the down-and-up template in Python, Java and TypeScript. It finds the lowest common ancestor of two nodes, reads the minutes of a message cascade as a tree's levels, shows why the path bending through a node must not be returned as its height, and ends with an exercise on saving a tree as one line and rebuilding it.
Contents
8 cardsnot started