datarekha

Trees, Traversals & Reconstruction

How a binary tree is walked four canonical ways — pre, in, post, and level-order — and why inorder plus one other order rebuilds the tree but preorder + postorder does not.

9 min read Intermediate GATE DA Lesson 59 of 122

What you'll learn

  • Tree vocabulary: root, child, leaf, depth, height — and what a binary tree is
  • Four traversals: preorder, inorder, postorder, and level-order (BFS)
  • Reconstruction: inorder plus pre or post rebuilds a tree; pre + post alone does not
  • Bounds: a height-h binary tree has at most 2^(h+1) − 1 nodes; n nodes need height at least floor(log2 n)

Before you start

A tree is a set of nodes connected by parent-child edges, with one root at the top and no cycles. The natural question is: in what order do you visit every node? That single question has four canonical answers, and GATE turns them into both traversal questions and the trickier reconstruction question — rebuilding a tree from its traversal sequences. The same traversals power real tools every day: a decision tree predicts by walking root-to-leaf, and parsers, file systems, and JSON walkers all lean on exactly these orders.

Vocabulary

A few terms you will see in every question:

  • Root — the single top node, no parent.
  • Child / parent — a node directly below / above another.
  • Leaf — a node with no children.
  • Depth of a node — edges from the root down to it (root has depth 0).
  • Height of the tree — the longest root-to-leaf path, counted in edges.

A binary tree restricts every node to at most two children, the left and the right child. The order of left vs right matters — that is what makes the traversals below distinct.

The four traversals

Three of the four are depth-first (DFS) and differ only in when the root is visited relative to its subtrees. The fourth is breadth-first (BFS).

  • Preorderroot, left, right (visit the root first).
  • Inorderleft, root, right (root in the middle).
  • Postorderleft, right, root (root last).
  • Level-order (BFS) — top to bottom, left to right, one depth level at a time (driven by a queue, not recursion).
12345rootleafPreorder (root,L,R)1 2 4 5 3Inorder (L,root,R)4 2 5 1 3Postorder (L,R,root)4 5 2 3 1Level-order (BFS)1 2 3 4 5
One tree, four orders. Nodes 4, 5, 3 are leaves; the height (longest root-to-leaf path) is 2.

How GATE asks this

Two patterns. (1) Traversal: given a drawn tree, write a named order, or read a value off it (a NAT for “number of leaves” or “height”). (2) Reconstruction (asked 2024): given two traversal sequences, rebuild the tree or report a property of it — and the subtle point is which pair suffices. Inorder plus preorder (or inorder plus postorder) rebuilds a binary tree uniquely; preorder plus postorder alone does not.

Worked example

Take the tree above: root 1, with left child 2 (whose children are 4 and 5) and right child 3 (a leaf). Reading off each order:

Preorder    (root, L, R):  1  2 4 5  3   →  1 2 4 5 3
Inorder     (L, root, R):  4 2 5  1  3   →  4 2 5 1 3
Postorder   (L, R, root):  4 5 2  3  1   →  4 5 2 3 1
Level-order (BFS):         1 | 2 3 | 4 5 →  1 2 3 4 5

Now reconstruct from inorder + preorder — the 2024 question:

Preorder = 1 2 4 5 3   → first element 1 is the ROOT
Inorder  = 4 2 5 1 3
                  ↑ split inorder at 1:
   left subtree  = 4 2 5      right subtree = 3

Recurse on the left (preorder 2 4 5, inorder 4 2 5):
   root = 2; inorder splits as 4 | 5 → children 4 (left) and 5 (right)
Right subtree is the single node 3.

That rebuilds exactly the original tree — uniquely. The root always comes from the front of preorder (or the back of postorder), and inorder supplies the left/right split around it.

Quick bounds to memorise: a binary tree of height h has at most 2^(h+1) − 1 nodes (a full tree). Conversely, n nodes need height at least floor(log2 n). Here n = 5, so the minimum possible height is floor(log2 5) = 2 — and this tree achieves it.

Quick check

Quick check

0/6
Q1For the lesson tree (root 1; left child 2 with children 4 and 5; right child 3, a leaf), how many leaves does it have?numerical answer — type a number
Q2The preorder of a binary tree is 1 2 4 5 3 and its inorder is 4 2 5 1 3. What is its postorder? Enter the LAST node of the postorder sequence (the value visited last).numerical answer — type a number
Q3A binary tree has height h = 3 (edges on the longest root-to-leaf path). What is the maximum possible number of nodes?numerical answer — type a number
Q4Which traversal pairs reconstruct a general binary tree UNIQUELY? (select all that apply)select all that apply
Q5Which statements about tree traversals are correct? (select all that apply)select all that apply
Q6For the lesson tree, what is its level-order (BFS) traversal? Enter the THIRD node visited.numerical answer — type a number

Practice this in an interview

All questions

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Related lessons

Skip to content