Skip to content
Veltoryx Technologies
FreeLesson 9.1Beginner3 min readUpdated 30 October 2026

The pattern: trees

Master binary trees, tree traversals (preorder, inorder, postorder, level order), and Binary Search Tree invariants.

01The tree pattern at a glance

A binary tree is a hierarchical data structure where every node has at most two children: a left child and a right child. Trees appear everywhere in computer science: file systems, DOM trees in browsers, database indices (B-trees), and expression parsers.

In technical interviews, tree problems test your ability to decompose recursive substructures, traverse graphs without cycles, and maintain invariants such as Binary Search Tree order.

text
       1 (root)
      /      2   3
    / \      4   5   6 (leaves)

02The two fundamental traversal paradigms

1. Depth-First Search (DFS)

DFS plunges deep into subtrees before backtracking. Depending on when you visit the current node relative to its children, there are three classic orders:

  • Preorder (Node -> Left -> Right): Visit the node first, then traverse the left and right subtrees. Ideal for copying trees, serializing trees, and prefix expressions.
  • Inorder (Left -> Node -> Right): Traverse the left subtree, visit the node, then traverse the right subtree. In a Binary Search Tree (BST), inorder traversal produces keys in strictly ascending sorted order!
  • Postorder (Left -> Right -> Node): Traverse both children first, then visit the parent. Essential when children must be evaluated before parent decisions can be made: calculating subtree heights, deleting trees, and post-fix expressions.

2. Breadth-First Search (BFS) / Level-Order Traversal

BFS visits nodes horizontal level by horizontal level from top to bottom. Using a FIFO queue (Deque<TreeNode>), you snapshot the queue size at the beginning of each level loop (int size = q.size()), processing an entire depth before advancing to the next.

03Binary Search Tree (BST) invariant

A binary tree is a valid Binary Search Tree (BST) if for every node:

  1. Every node in its left subtree has a value strictly smaller: left.val < node.val.
  2. Every node in its right subtree has a value strictly greater: right.val > node.val.
  3. Both left and right subtrees are also valid BSTs.

A critical trap: checking only node.left.val < node.val < node.right.val is insufficient. The BST property is a global ancestor bound: every node must sit within an open range (low, high) passed down from all its ancestors.

04Tree problem classification

CategoryPrimary strategyClassic problems
Traversal ordersExplicit stack / recursionPreorder, Inorder, Postorder, Zigzag
Structural propertiesBottom-up postorderMax Depth, Same Tree, Symmetric Tree
Path problemsTop-down passing remainderPath Sum, Path Sum II, Root-to-Leaf paths
Level inspectionFIFO queue with level snapshotLevel Order I & II, Right Side View
BST algorithmsInorder walk / range boundariesValidate BST, Min Distance, Kth Smallest
Construction & SerializationPreorder bounds / divide-and-conquerSorted Array to BST, BST Serialization