← Back to the trees page

Binary Tree

Each node has at most two children. No ordering guarantee — the value is in the shape, and traversal order is the main tool.

What to know

  • Preorder copies structure, inorder is meaningful mostly for BSTs, postorder deletes safely, level-order uses BFS.
  • Height, diameter, and balance checks are all postorder computations.
  • Serialization (preorder + null markers) round-trips any binary tree.

In the wild: Expression trees in compilers, decision trees, DOM subtrees, Huffman code trees.

Algorithms to reach for

Four traversals (pre/in/post/level)

O(n)

Visit orders for copying, evaluating, deleting, printing

Diameter via postorder

O(n)

Longest node-to-node path (LeetCode 543)

LCA in a binary tree

O(n)

Lowest common ancestor without parent pointers (LeetCode 236)

Serialize/deserialize

O(n)

Ship trees across a network (LeetCode 297)