Trees: root, leaf, depth, height, subtree
A tree has a root, children, leaves, depth and height. Hover over a tree to see each term and measure depth with rulers.
The idea
Folders on a disk form a tree. Every folder sits inside exactly one parent folder, all the way up to /.
Trees show up wherever things nest: folders, web pages, family trees, company charts. The same few words describe all of them.
Intuition
The top node A is the root. B and C hang below A, so they are its children and A is their parent.
Each node except the root has exactly one parent. The lines between parent and child are called edges.
D, F, G and H have no children. Nodes with no children are leaves; the others are internal nodes.
The depth of a node counts the edges from the root down to it. G is 3 edges below A, so its depth is 3.
All nodes at the same depth form a level. The root alone is level 0.
A node together with everything below it is a subtree. The subtree of E holds E, G and H.
A subtree is a tree in its own right, with E as its root. That is why tree code so often calls itself on each child.
The height of a tree is the depth of its deepest node. The path A, B, E, G has 3 edges, so the height is 3.
The height of a node is the longest path down from it to a leaf. The tree's height is the height of its root.
Complexity
Height visits each node once, so it costs O(n) time. The chart shows h, the recursion depth and extra space: n - 1 for a chain, about log2 n when bushy.
A tree with n nodes has height between about log2 n (bushy) and n - 1 (a chain). So h decides how deep the call stack grows.
Pitfalls
A single node is both the root and a leaf. Its height is 0 when you count edges, as this lesson does.
Some books and problems count nodes instead, so one node has height 1 and an empty tree has height 0. Read which one a problem asks for.
Depth and height are not the same number. Depth counts edges down from the root; height counts edges down to the deepest leaf.
B is 1 edge below the root, so its depth is 1. Its deepest leaf G is 2 edges below it, so its height is 2.
Variations
The same terms work when a node has any number of children. A binary tree limits each node to at most two.
Code
Pseudocode
height(node)
if node = null: return -1
hl ← height(node.left)
hr ← height(node.right)
return 1 + max(hl, hr)
Python
def height(node):
if node is None:
return -1
hl = height(node.left)
hr = height(node.right)
return 1 + max(hl, hr)
JavaScript
function height(node) {
if (node === null) return -1;
const hl = height(node.left);
const hr = height(node.right);
return 1 + Math.max(hl, hr);
}
Practice problems (7)
Label the parts of a tree Warm-up
Look at the tree in the example. Match each term to the node or number it describes.
Compute the height Warm-up
Each node's height is 1 more than the larger height of its children; a leaf has height 0. Follow the trace on the tree below and fill in each height before the step plays.
Count the leaves Standard
A leaf is a node with no children. Count the leaves of the tree below, then follow the trace as it checks every node.
Height of a binary tree Standard
Given the root of a binary tree, return its height: the number of edges on the longest path from the root down to a leaf. A single node has height 0 and an empty tree has height -1.
Count the leaves in code Standard
Given the root of a binary tree, return the number of leaves: nodes with no left child and no right child.
Count the nodes at depth d Standard
Given the root of a binary tree and a number d, return how many nodes have depth exactly d. The root has depth 0, its children depth 1, and so on. Return 0 when the tree has no level d.
Nodes at depth d in a full tree Challenge
In this tree every level is completely filled: each internal node has two children and all leaves share one depth. Predict how many nodes each level holds, then give the rule for any depth d.
Related topics
Next on the path: Binary trees and how they are stored