Tree Traversal

01 / The one-sentence essence

Every tree-traversal algorithm visits every node exactly once — the only thing that varies is when the visit happens relative to the recursive descent into children.
Problemtraverse a binary tree in a given orderTree[1, 2, 3, 4, 5, ·, 6]Modepreorder
123456
output— nothing yet —
Depth-first — preorder visits the node before recursing into children.
step
0 / 7
visiting
visited
0 / 6
mode
preorder
0 / 7

02 / The pattern signature

# depth-first — three variants differ only in where "visit" sitstraverse(node):if node is null: returnvisit(node) // preordertraverse(node.left)visit(node) // inordertraverse(node.right)visit(node) // postorder# breadth-first — iterative, queue-basedlevel_order(root):q [root]while q not empty:node q.dequeue(); visit(node)enqueue node.left, node.right

03 / When to recognize this pattern

"in-order on a BST"
The sorted output of a binary search tree comes from inorder — that's a property of BSTs, not of inorder in general.
"level by level"
You need to process the tree row by row — node depths, zigzag output, "find the right side of the tree". Level-order via a queue is the tool.
"bottom-up / children-first"
You can't decide a node's answer until its children's answers are known — subtree sums, heights, paths. Postorder guarantees children are visited first.
"serialize / clone"
Producing a string representation of a tree, or copying one node-by-node. Often preorder with explicit null markers — it captures the structure unambiguously.

04 / Common pitfalls

Using inorder when postorder is required.
Anything that needs a child's answer to compute the parent's — subtree sums, heights, diameters — must visit children before the parent. That's postorder. Inorder will visit some children too late.
Forgetting the null check.
The first line of the recursive traversal is if node is null: return. Missing it dereferences null at every leaf's missing children. The animation makes this explicit — null subtrees just don't render.
Confusing the call stack with the output list.
The recursion's natural call stack is not the order of visits. A node is on the call stack from before its preorder visit through after its postorder visit — three different "moments" of the same node.

05 / Go practice — on LeetCode

medium42

01Validate Binary Search Tree— LC 9802Recover Binary Search Tree— LC 9903Binary Tree Level Order Traversal— LC 10204Binary Tree Zigzag Level Order Traversal— LC 10305Construct Binary Tree from Preorder and Inorder Traversal— LC 10506Construct Binary Tree from Inorder and Postorder Traversal— LC 10607Binary Tree Level Order Traversal II— LC 10708Path Sum II— LC 11309Sum Root to Leaf Numbers— LC 12910Binary Search Tree Iterator— LC 17311Binary Tree Right Side View— LC 19912Kth Smallest Element in a BST— LC 23013Lowest Common Ancestor of a Binary Search Tree— LC 23514Lowest Common Ancestor of a Binary Tree— LC 23615Binary Tree Vertical Order Traversal— LC 31416House Robber III— LC 33717N-ary Tree Level Order Traversal— LC 42918Path Sum III— LC 43719Serialize and Deserialize BST— LC 44920Delete Node in a BST— LC 45021Convert BST to Greater Tree— LC 53822Maximum Width of Binary Tree— LC 66223Insert into a Binary Search Tree— LC 70124Smallest Subtree with all the Deepest Nodes— LC 86525Construct Binary Tree from Preorder and Postorder Traversal— LC 88926Distribute Coins in Binary Tree— LC 97927Smallest String Starting From Leaf— LC 98828Construct Binary Search Tree from Preorder Traversal— LC 100829Binary Search Tree to Greater Sum Tree— LC 103830Lowest Common Ancestor of Deepest Leaves— LC 112331Tree Diameter— LC 124532Smallest Common Region— LC 125733Deepest Leaves Sum— LC 130234Maximum Product of Splitted Binary Tree— LC 133935Longest ZigZag Path in a Binary Tree— LC 137236Balance a Binary Search Tree— LC 138237Count Good Nodes in Binary Tree— LC 144838Diameter of N-Ary Tree— LC 152239Lowest Common Ancestor of a Binary Tree II— LC 164440Lowest Common Ancestor of a Binary Tree III— LC 165041Create Binary Tree From Descriptions— LC 219642Most Profitable Path in a Tree— LC 2467