Home

Levelorder

Levelorder, or level-order traversal, is a method of visiting nodes in a tree or graph in order of increasing depth, beginning at a designated root node and proceeding level by level from top to bottom and left to right within each level. In trees, level-order is a form of breadth-first traversal.

Implementation typically relies on a queue. The root is enqueued; while the queue is not empty, a

For graphs, the traversal is known as breadth-first search (BFS). To avoid visiting the same node multiple

Complexity: Visiting n nodes requires O(n) time in a tree or graph. The space usage is O(w),

Common uses include serializing and deserializing trees in level-order form, printing trees level by level, and

See also: breadth-first search, BFS, and other tree traversal orders such as pre-order, in-order, and post-order.

node
is
dequeued
and
processed,
and
its
children
are
enqueued
in
a
defined
order.
In
binary
trees,
the
left
child
is
enqueued
before
the
right
child.
times,
a
set
of
visited
nodes
is
maintained.
where
w
is
the
maximum
width
of
the
structure;
in
the
worst
case,
O(n).
computing
shortest
paths
in
unweighted
graphs
via
BFS.
Levelorder
representations
are
also
used
in
various
programming
tasks
and
interview
problems
that
require
processing
nodes
by
level
rather
than
by
a
depth-first
pattern.
Levelorder
is
a
practical
approach
for
problems
where
level-by-level
information
is
important
or
where
a
shallow,
wide
exploration
is
desired.