juniorTrees

What is a leaf node in a tree?

Updated Apr 28, 2026

Short answer

A leaf node is a node in a tree that does not have any children. It is the endpoint of a path through the tree, meaning no other nodes exist below it. In a binary tree, a leaf node has both its left and right child pointers set to null.

Deep explanation

In a tree data structure, nodes are connected in a parent-child relationship. A node that has one or more child nodes is called an internal node (or non-leaf node), while a node with no children is called a leaf node.

For example, consider this binary tree:

TypeScript
A
/ \
B C
/ \
D E
  • A is the root node.
  • B and C are internal nodes because they have children or are part of the tree structure below them.
  • D and E are leaf nodes because they do not have any children.

The main characteristic of a leaf node is:

TypeScript
number of children = 0

In a binary tree, this can be checked by verifying that both child references are empty:

Python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def is_leaf(node):
return node.left is None and node.right is None

Leaf nodes are important because many tree algorithms use them as stopping points. When traversing a tree, recursion often ends when it reaches a leaf because there are no further children to explore.

Common uses of leaf nodes include:

  • Tree traversal: Algorithms such as depth-first search often perform special processing when they reach leaves.
  • Binary search trees: Leaf nodes represent values that do not have any further descendants.
  • File systems: Files are often represented as leaf nodes, while folders are internal nodes containing other files or folders.
  • Decision trees: The leaves represent final outcomes or predictions.

A tree can contain:

  • A single node that is both the root and a leaf.
  • Many leaf nodes at different depths if the tree is not balanced.
  • No fixed number of leaf nodes; it depends on the tree structure.

For a binary tree with n nodes, the number of leaf nodes can vary from 1 (a completely skewed tree) up to approximately half of the nodes (in a full binary tree).

Real-world example

A computer's file system is a simple example of a tree structure:

TypeScript
Computer
├── Documents
│ ├── Resume.pdf
│ └── Notes.txt
└── Photos
└── Vacation.jpg

Here:

  • Computer, Documents, and Photos are like internal nodes because they contain other items.
  • Resume.pdf, Notes.txt, and Vacation.jpg are leaf nodes because they do not contain anything inside them.

A program that searches for all files could traverse the tree and perform an action whenever it reaches a leaf:

Python
def find_files(node):
if node.is_file:
print(node.name)
return
for child in node.children:
find_files(child)

In this example, files are the leaf nodes where the traversal finishes.

Common mistakes

  • * Confusing a leaf node with the root node. A root node can also be a leaf if the tree contains only one node.
  • * Assuming a leaf node must be at the deepest level of the entire tree. A leaf only needs to have no children
  • different leaves can exist at different depths.
  • * Thinking a leaf node must have a specific value or property. Being a leaf depends only on its number of children.
  • * Forgetting that leaf node definitions depend on the tree structure. A node that is a leaf today may become an internal node if children are added.
  • * Assuming every tree must have multiple leaf nodes. A tree with one node has exactly one leaf node.

Follow-up questions

  • How do you find all leaf nodes in a binary tree?
  • What is the difference between a leaf node and an internal node?
  • Can the root node also be a leaf node?
  • How are leaf nodes used in a binary search tree?
  • What is the time complexity of finding all leaf nodes in a tree?

More Trees interview questions

View all →