Recipe
Find parent, children, level, and height in a rooted tree
Use when: you're given a rooted tree and asked to name a vertex's parent, children, or ancestors, its level, or the tree's height
Goal: every relationship and level value read directly off the tree, with no guessing
Taught in Rooted trees
Quick recipe
-
1
Follow tree edges upward from the target vertex until reaching the root. A rooted tree guarantees exactly one such path.
Example
For vertex D in the labeled tree (root R; children A, B, C; A's children D, E; C's child F), the path back to the root is D \to A \to R.
Why: every vertex in a rooted tree has a unique path to the root, so this path always exists and is never ambiguous.
-
2
The vertex immediately above the target on the path is its parent. Every other vertex on the path is an ancestor.
Example
On the path D \to A \to R, A sits directly above D, so A is D's parent, and both A and R are D's ancestors.
- the target vertex is the root itself — it has no parent and no ancestors, and its level is 0.
- the target vertex is any other vertex — it always has exactly one parent, the vertex found in Step 1.
Why: parent names the one vertex directly above the target, while ancestor extends that relationship to every vertex farther up the same path.
-
3
A vertex's children are the neighbors whose own path to the root passes through it. In a drawn tree, these sit directly below it, joined by an edge.
Example
A's neighbors below it are D and E, so A's children are D and E.
Why: a child is exactly a neighbor farther from the root, the reverse of the parent relationship.
-
4
The level of a vertex is the number of edges on its unique path back to the root, that is, the path's length.
Example
A's path to the root is A \to R, one edge, so A sits at level 1. D's path is D \to A \to R, two edges, so D sits at level 2.
Why: level measures distance from the root, and distance in a tree is exactly the number of edges on the one connecting path.
-
5
List every path from the root to a leaf, count the edges on each, and take the largest count. That largest value is the height.
Example
R \to B has length 1. R \to A \to D and R \to A \to E each have length 2. R \to C \to F also has length 2, so the largest is 2.
Why: height is defined as the largest level any vertex reaches, so it is a maximum over every root-to-leaf path, not an average.
Common mistakes
- Naming an ancestor two or more steps up the path as if it were the parent.
- Computing height by averaging path lengths instead of taking the largest one.
- Confusing a vertex's children with its full set of descendants further down the tree.
- Miscounting level by counting the vertices on the path instead of the edges between them.