Introduction
A tree is a hierarchical structure made of nodes joined by parent-child links, with one special node, the root, and no cycles. Trees model anything with a "contains" or "is part of" relationship: directory hierarchies, the parse tree a compiler builds for an expression, the index of a database file, and the decision process of a sorting algorithm.
A binary tree restricts every node to at most two children, called the left child and the right child. That restriction makes binary trees easy to store and easy to reason about, and it is the base for three structures in the GATE syllabus: binary search trees, binary heaps and expression trees used in stack applications.
This note covers the vocabulary, the counting facts that GATE numerical questions rely on, the main kinds of binary tree, how a binary tree is stored, and which traversal sequences fix a tree uniquely. Traversal algorithms themselves are covered in Binary Tree Traversals.
Prerequisites
- Recursion: almost every tree definition and algorithm is recursive.
- Pointers and Structures, Unions and Enumerations: a linked node is a structure holding pointers.
- Basic graph terms help but are not required: Graph Theory Fundamentals defines a tree as a connected acyclic graph.
Core concepts
Rooted trees and terminology
A rooted tree is either empty or consists of a root node together with zero or more disjoint subtrees, each of which is itself a rooted tree whose root is a child of the root.
| Term | Meaning |
|---|---|
| Parent, child | If an edge joins and and is closer to the root, is the parent of |
| Siblings | Nodes with the same parent |
| Leaf | A node with no children (some texts also call it an external node; see the extended-tree convention under "Null links") |
| Internal node | A node with at least one child |
| Degree of a node | Number of children of that node (in a rooted tree) |
| Ancestor, descendant | is an ancestor of if lies on the path from the root to |
| Depth (level) of a node | Number of edges from the root to the node; the root has depth 0 |
| Height of a node | Number of edges on the longest downward path from the node to a leaf; a leaf has height 0 |
| Height of a tree | Height of its root |

Height convention used in all Gate CS Mastery notes. Height counts edges. A single node has height 0 and the empty tree has height . Some books count nodes instead, which adds 1 to every height. Before applying any formula in a question, check which convention the question uses; every height value shifts by 1, so height-dependent formulas need the matching convention; formulas unrelated to height do not change.
A non-empty tree with nodes has exactly edges, because every node except the root has exactly one edge to its parent.
Binary trees
A binary tree is either empty, or a root together with a left subtree and a right subtree, each of which is a binary tree. The left/right distinction matters: a root with only a left child and a root with only a right child are two different binary trees. This is what separates a binary tree from an ordered tree in which a node merely has "at most two" children.
Kinds of binary tree
Names in this area differ between textbooks. The definitions below are the ones used throughout these notes; if a text uses a different meaning, check whether it defines the term.
| Kind | Definition used here | Example property |
|---|---|---|
| Full (proper, strictly binary) | Every node has 0 or 2 children | |
| Perfect | Full, and all leaves at the same depth | |
| Complete | Every level full except possibly the last, which is filled from the left | height |
| Skewed (degenerate) | Every internal node has exactly one child | height |
| Balanced (informal) | Height is ; exact meaning depends on the scheme, for example AVL | see AVL Trees |
Here means . CLRS calls a perfect tree "complete" (Appendix B.5.3) and a heap-shaped tree "nearly complete" (Section 6.1); these notes use "complete" in the sense of the table.

Counting facts and their proofs
Let be the number of nodes, the height (edge convention), and the numbers of nodes with 0, 1 and 2 children.
Maximum and minimum number of nodes for a given height
Level of a binary tree holds at most nodes, since each node on level has at most two children. Summing over levels to :
Equality holds exactly for the perfect tree. The minimum for height is nodes (a skewed chain).
Turned around, a binary tree with nodes has height
and for . Example: with , the minimum height is and the maximum is 99. The number of distinct binary trees with nodes and height is , since each of the non-root nodes is independently a left or a right child.
Leaves versus two-child nodes:
Count edges two ways. Every node except the root has one parent edge, so there are edges. Each node contributes as many edges as it has children, so there are edges. With :
This holds for every non-empty binary tree, whatever its shape, and the number of one-child nodes does not appear. Two consequences:
- A full binary tree has , so : it always has an odd number of nodes.
- Worked example: a binary tree has 10 leaves and 5 nodes with one child. Then and .
Null links:
For a nonempty tree (), in the linked representation each node has two child pointers, pointers in total. Exactly of them point to a node (one per edge), so
An empty tree has zero node child fields, edges and data-node leaves, and height ; its separate root handle is null, not a child field. For the 24-node tree above there are 25 null links. Threaded binary trees reuse these null fields to store traversal links.
Extended-tree convention. Some texts (and discussions of "extended binary trees") replace each null link by a square external node. In that view there are exactly external nodes, every original node is internal, and external nodes are the newly added leaves of the extended tree, not the original tree's data-node leaves. Check which meaning is in use.
Number of distinct binary trees: the Catalan numbers
Let be the number of structurally different binary trees with nodes (unlabelled shapes). Choosing a shape means choosing a left subtree with nodes and a right subtree with nodes:
This is the Catalan recurrence, with closed form
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 5 | 14 | 42 | 132 | 429 |

Read the question carefully for what is being counted:
- Unlabelled shapes with nodes: .
- Labelled binary trees with distinct labels placed anywhere: (for : ).
- Binary search trees on distinct keys: , because the BST ordering fixes where each key goes once the shape is chosen (see Binary Search Trees).
Representing a binary tree
Linked representation
struct node {
int key;
struct node *left; /* NULL if no left child */
struct node *right; /* NULL if no right child */
};Each node costs one key and two pointers; a parent pointer is added when upward moves are needed (for example, finding a successor without a stack). Space is for any shape.
Array (sequential) representation
Number the positions level by level, left to right, starting at 1 for the root. With 1-based indexing, for the node at index :
With 0-based indexing the formulas become , and . Always state which base is in use.
The array form has no wasted slots for a complete binary tree, which is why binary heaps use it. For other shapes it wastes space: a right-skewed tree of height needs an array of size to hold only nodes.
For a complete binary tree with nodes stored this way, the leaves are exactly the indices , so there are leaves. Example: gives leaves at indices 6 to 10, five leaves.
Which traversals determine a tree uniquely
A traversal lists the nodes in a fixed order: preorder (node, left, right), inorder (left, node, right), postorder (left, right, node) and level order (by depth, left to right). Assume all keys are distinct.
| Given pair | Unique tree? | Why |
|---|---|---|
| Inorder + preorder | Yes | Preorder names the root; inorder splits the rest into left and right subtrees; recurse |
| Inorder + postorder | Yes | Same, with the root last in postorder |
| Inorder + level order | Yes | Level order names the root first; inorder splits; recurse on the subsequences |
| Preorder + postorder | Not in general | They cannot tell a lone left child from a lone right child. Exactly trees share one preorder and postorder pair, where is the number of one-child nodes, so the pair is unique exactly when |
| Preorder + postorder, full binary tree | Yes | Every internal node has two children, so the split is forced |
| Level order + preorder, or level order + postorder | Not in general | Already fails for two nodes: root 1 with child 2 on either side gives the same level order, preorder and postorder |
| Any single traversal | No in general | For a BST with distinct keys, preorder alone, postorder alone or level order alone suffices, because inorder is the sorted order |
The smallest counterexample: root 1 with left child 2, and root 1 with right child 2. Both have preorder 1, 2 and postorder 2, 1.

Worked example: rebuilding from preorder and inorder
Preorder: A B D E G C F H. Inorder: D B G E A C H F.
- Preorder starts with A, so A is the root. In the inorder sequence, D B G E is left of A and C H F is right of A.
- Left part: the next preorder symbols B D E G belong to it. B is its root; inorder D | B | G E puts D on the left and G E on the right.
- Inside G E: preorder lists E before G, so E is the root, and inorder G E puts G as E's left child.
- Right part: preorder C F H, inorder C H F. C is the root with nothing on its left; F H form its right subtree, with root F and H as F's left child.

Check: the rebuilt tree has postorder D G E B H F C A and level order A B C D E F G H.
BUILD(pre[1..n], in[1..n])
if n = 0 then return NIL
r <- pre[1]
k <- position of r in in[1..n]
t <- new node with key r
t.left <- BUILD(pre[2..k], in[1..k-1])
t.right <- BUILD(pre[k+1..n], in[k+1..n])
return tSearching for the root in the inorder array each time makes this in the worst case (a skewed tree); using index-range/view arguments (no subarray copying) and a key-to-position map gives with lookup (expected for hashing, or worst-case for suitable direct indexing). The map/output use storage and recursion uses stack. Naive linear lookup is best-case overall and worst-case; average-case analysis requires an input model and is deferred.
Important properties and observations
- For a nonempty tree (): The edge count is always , whatever the shape.
- For a nonempty tree (): needs only "binary tree"; it does not need full or complete.
- For a nonempty tree (): A full binary tree with leaves has internal nodes and nodes.
- For a nonempty tree (): A complete binary tree with nodes has height and leaves.
- For a nonempty tree (): A perfect binary tree of height has leaves and internal nodes.
- Recursive algorithms on a tree of height use stack space: on a balanced tree but on a skewed one.
Common misconceptions.
- "Complete" and "full" are different. A complete tree can have one node with a single (left) child; a full tree cannot.
- "Number of binary trees with nodes" means shapes () unless the question says the nodes are labelled.
- Preorder plus postorder does not always fix a tree; inorder is needed unless the tree is full.
GATE examination perspective
- Counting questions. Expect to use , , null links and Catalan numbers. First identify the height convention and whether nodes are labelled.
- Reconstruction questions. Given two traversals, rebuild the tree and report a third traversal or the height. Rebuild level by level and check by recomputing both given sequences.
- Array index questions. Confirm whether the array is 0-based or 1-based before using parent and child formulas.
- Trap: height off by one. If the answer choices differ by exactly 1, the convention is the point being tested.
- Trap: assuming "binary tree" means "binary search tree". Ordering facts apply only to BSTs.
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, Section 6.1 (heaps as "nearly complete" binary trees), Section 10.4 (representing rooted trees) and Appendix B.5 (trees; B.5.3 binary and positional trees, including its "complete k-ary tree" definition) and Problem 12-4 (number of different binary trees: the Catalan numbers).
- P. Morin, Open Data Structures (Python edition), Section 6.1 "BinaryTree: A Basic Binary Tree", (consulted 2026-11-09).