Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Tree

Дерево

A data structure whose nodes are connected so that there is exactly one route without repeated nodes between any two of them. Choose one node as the root, and every other node has exactly one parent.

What is a tree?

Think of a table of contents: a book contains chapters, and each chapter can contain sections. Every section has one place in the hierarchy. If you give a single section node two parents rather than making two separate entries, the structure is no longer a tree, even if all links still point downward.

In graph theory, a tree is a connected undirected graph with no cycles. Connected means you can reach every vertex from any starting vertex; having no cycles means there is only one simple path (a path that does not repeat vertices) between any pair. A tree with n vertices has exactly n − 1 edges. A disconnected collection of trees is called a forest.

Roots, leaves, and representation

A graph-theoretic tree does not have to start anywhere in particular. Picking a root gives it a hierarchy: for any other node, its parent is the next node on the path back to the root. A node with no children is a leaf. A node and all its descendants form a subtree.

Depth measures how far a node is from the root; height is the greatest depth in the tree. If you measure both in edges, the root has depth zero and a one-node tree has height zero. Some coding problems count nodes instead, so check the convention before choosing a base case.

In code, each node might store a list of its children. If the input instead gives you undirected edges, build adjacency lists and skip the edge back to the parent during traversal. The same tree can look quite different when you choose a different root.

Example: visit nodes level by level

from collections import deque

def level_order(root):
    if root is None:
        return
    queue = deque([root])
    while queue:
        node = queue.popleft()
        yield node.value
        queue.extend(node.children)

This is breadth-first search (BFS). Appending children to the back of the queue ensures that remaining nodes at the current depth are processed first. Visiting all n nodes takes O(n) time; the queue uses O(w) space, where w is the maximum number of nodes on a level. Depth-first search (DFS), by contrast, explores one branch before moving to the next. A recursive DFS on a chain-shaped tree can require O(n) call-stack space.

Where trees show up and what interviewers test

Compilers use syntax trees to represent how expressions and statements fit together. Browsers expose a document's nested structure through the DOM. Specialized trees add rules for particular operations: a trie uses shared prefixes to organize keys, while a binary search tree orders keys so that a lookup can skip an entire subtree. Having at most two children per node makes a tree binary, but says nothing about key order or balance.

Common interview tasks include finding the height, printing one list per level, and validating a binary search tree. For a level-by-level task, record the queue length at the start of each level so newly enqueued children are processed in the next round. Test your solution on an empty input, a single node, and a long chain: those cases expose mistakes that a small balanced example may hide.