A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Tree Traversals

College Depth 84 in the knowledge graph I know this Set as goal
236topics build on this
437prerequisites beneath it
See this on the map →
Binary TreesTree Structure and Node Properties+2 moreAbstract Syntax Trees (ASTs)Binary Search Trees+3 more
traversal inorder preorder postorder level-order

Core Idea

Tree traversal visits every node in a tree exactly once. Depth-first traversals include inorder (left → root → right), preorder (root → left → right), and postorder (left → right → root); each visits nodes in a different order suited to different applications. Breadth-first (level-order) traversal visits nodes level by level using a queue. Inorder traversal of a binary search tree yields elements in sorted order, making it especially useful for validation and enumeration.

How It's Best Learned

Implement all four traversals both recursively and iteratively (using an explicit stack or queue). For each, predict the output order by hand before running the code, then verify.

Common Misconceptions

Explainer

You already understand binary trees — each node has at most two children, called left and right. A tree traversal is a systematic way to visit every node exactly once, and the order in which you visit them determines what the traversal is useful for. There are four standard traversals, three depth-first and one breadth-first, and each answers a different question about the tree's contents.

The three depth-first traversals differ only in when they process the current node relative to its children. Preorder (root, left, right) visits the current node first, then recurses into the left subtree, then the right. This naturally produces a top-down view — you see parents before children, making preorder ideal for copying a tree or producing a prefix representation of an expression. Inorder (left, root, right) recurses into the left subtree first, then visits the current node, then the right subtree. For a binary search tree, this visits nodes in ascending sorted order — the left subtree contains smaller values, the root is next, and the right subtree contains larger values. Postorder (left, right, root) recurses into both subtrees before visiting the current node, giving a bottom-up view — you process children before parents, making it natural for computing aggregate properties (like subtree size or height) and for safely deleting a tree.

Breadth-first (level-order) traversal visits all nodes at depth 0, then depth 1, then depth 2, and so on. Instead of recursion, it uses a queue: enqueue the root, then repeatedly dequeue a node, process it, and enqueue its children. This produces a left-to-right, top-to-bottom sweep of the tree and is useful when you need to process nodes by level — for example, finding the shallowest node satisfying some condition, or printing a tree level by level.

The recursive implementations are concise — each depth-first traversal is about three lines of code. But recursion uses the call stack, and a deeply unbalanced tree (essentially a linked list) can cause stack overflow. The iterative versions use an explicit stack (for depth-first) or queue (for breadth-first), giving you direct control over memory. For iterative inorder, you push nodes as you go left, pop when there's nothing left to push, process the popped node, then move right. Mastering both recursive and iterative forms is important because the iterative versions reveal what the recursion is actually doing — managing a frontier of nodes yet to be visited — which is the same pattern you will encounter in graph traversal algorithms like depth-first search and breadth-first search.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree Traversals

Longest path: 85 steps · 437 total prerequisite topics

Prerequisites (4)

Leads To (5)