Skip to content
DSA in JavaScript
← All patterns

Binary Tree Traversal

0/18 solved

What it is

Visit every node of a tree in a fixed order: before its children (pre), between them (in) or after them (post).

Used for

Copying, searching or validating a tree, getting values in sorted order from a BST, serialisation.
791152

Step 1 of 5

A tree has no single first-to-last order, so we pick one. The three common orders differ only in when a node is visited compared with its children.

You see

  • Visit every node in a given order
  • Build or serialise a tree
  • Properties like depth, symmetry or equality

Learn it in

TreeAVL TreeRed–Black TreeDepth-First Search (tree)Breadth-First Search (tree)Binary Search Tree

Template

function inorder(node, visit) {  if (!node) return;  inorder(node.left, visit);  visit(node.val);  inorder(node.right, visit);}

Practice problems