Crossings: pre in post

Beginner Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

Three depth traversal orders are common. One starts at the node and then visits the left and right subtrees. Another visits the left subtree first, then the node, and then the right. The third concludes the node only after both subtrees.

For a search tree, the left-node-right order yields the sorted key sequence. The node-first order is suitable for serializing the structure.

Example tree: root 2, left 1, right 3. The sorted traversal results in 1, 2, 3.

Diagram

Inorder as a sequence: Binary Tree Inorder Traversal [1].

Where used

Serialization before children, sorted output in search trees, expression evaluation, and releasing after children, build systems, and dependency resolution. The choice of traversal order is a product decision, not just an exercise.

Depth

Depth traversals differ by the time the current node is processed relative to its subtrees. Processing before both children is suitable for serializing structure, processing between children yields sorted keys in search trees, and processing after both children supports bottom-up evaluations.

All three variants visit every reachable node once and require time proportional to the number of nodes. The additional storage depends on the height, because at most one root path needs to be active at the same time.

An iterative implementation stores nodes together with the state yet to be executed. Simply storing nodes is often not sufficient for variants processing after one child.

Difficulty levels

  1. Determine traversal orders for a small tree.
  2. Choose the processing time based on an application goal.
  3. Iteratively represent recursive control states.

Pitfalls

The order of values does not always determine the tree uniquely without additional information. In the iterative form, children are often stored in the wrong order.


Sources

University approvals: 0
Tasks
Question 1

Which traversal visits the root first?

Question 2

Which traversal on a binary search tree typically yields ascending keys?

Question 3

When is the current node processed in a bottom-up evaluation?

Question 4

Return the values of a binary tree in inorder sequence.

Hint

Values are appended to a List<Integer> with result.add(node.value). A private helper method can receive the same list.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Best
Best
BestBuddy