Formula Bro

Math and physics formulas, explained like you are new.


Perfect Binary Tree Nodes

In one sentence: The formula counts every node in a perfect binary tree, a branching diagram in which each row is completely full, using only the number of rows below the top.

N=2h+1−1N = 2^{h+1} - 1

Read it out loud: “N equals 2 to the power of h plus 1, minus 1. N is the total number of nodes, the boxes of the tree. The raised 2 is a power, which means 2 multiplied by itself, the number of times shown in the exponent. The exponent is h plus 1, and the minus 1 is done last. h is the height of the tree, the number of lines from the top box down to the bottom.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Powers of two, like 232^{3}

A power shows repeated multiplication. 232^{3} means 2 multiplied by itself 3 times: 2×2×2=82 \times 2 \times 2 = 8. The small raised number is the exponent. Each time the exponent goes up by one, the power doubles: 20=12^{0} = 1, 21=22^{1} = 2, 22=42^{2} = 4, 23=82^{3} = 8. When powers of the same number are multiplied, the exponents add, so 23×21=24=162^{3} \times 2^{1} = 2^{4} = 16.

Tree, node, root, child and leaf

A tree is a diagram of boxes joined by lines, with one box at the top. Each box is a node. The top box is the root. A node directly below another is its child, and a node with no children at the bottom is a leaf. A binary tree is one where each node has at most two children, one on the left and one on the right.

Height, measured in edges

An edge is one line joining two nodes. The height of a tree is the number of edges on the longest path from the root down to a leaf. The root alone has height 0, and a root with two children has height 1. Height counts the lines, not the boxes, and that choice changes the exponent in the formula by one.

Levels, or rows

A level is a row of nodes that are all the same number of edges below the root. Level 0 is the root, level 1 holds its children, and so on. The bottom level holds the leaves. A tree of height hh has h+1h + 1 levels, numbered from 0 to hh.

Perfect tree and the dots ⋯\cdots

A perfect binary tree is one in which every node that is not a leaf has exactly two children, and every leaf sits on the same bottom level. Drawn in rows, every row is completely full. The three dots ⋯\cdots mean ‘continue in the same pattern’, so 1+2+4+⋯+2h1 + 2 + 4 + \cdots + 2^{h} means add every power of 2 from 202^{0} up to 2h2^{h}.

What each symbol means

SymbolMeaning

NN

The total number of nodes in the tree: every box, counting the root, the inner nodes and the leaves. It is a plain count with no unit. The example tree has 15 nodes.

hh

The height of the tree: the number of edges on the longest path from the root down to a leaf. It is a plain count with no unit, and the root alone has height 0. The example uses height 3, which gives four levels, numbered 0 to 3.

The formula piece by piece

2h2^{h}

Start with the bottom level. Each level has double the nodes of the level above it, so level hh holds 2h2^{h} nodes, and these are the leaves. At height 3 the bottom level holds 23=82^{3} = 8 leaves.

2h+12^{h+1}

The exponent is one bigger than the height, because a tree of height hh has h+1h + 1 levels. Doubling the bottom level’s count does not give the total, but 2h+12^{h+1} is the number needed before subtracting one. At height 3 it is 24=162^{4} = 16.

N=2h+1−1N = 2^{h+1} - 1

The whole formula as one sentence: the number of nodes in a perfect binary tree equals two to the power of one more than its height, minus one. For height 3 that is 24−1=16−1=152^{4} - 1 = 16 - 1 = 15 nodes.

1+2+4+⋯+2h1 + 2 + 4 + \cdots + 2^{h}

The same total written level by level. The root has 1 node, level 1 has 2, level 2 has 4, and so on, down to the bottom level with 2h2^{h} nodes. Adding every level gives the same count as the formula, which is the second way to see why the formula works.

What the formula is doing, and why

A binary tree is a set of nodes where each node has at most two children. A perfect binary tree is one where every inner node has exactly two children and every leaf sits at the same depth. Drawn in rows, every level is completely full, with no gaps.

Counting levels. Define the height hh as the number of edges from the root down to the bottom. The root alone has height 0. Level kk is the row of nodes that are kk edges below the root, so level 0 is the root. The bottom level is level hh, and there are h+1h + 1 levels in all.

Why doubling? Each node on one level has two children on the next level. So level 0 has 20=12^{0} = 1 node, level 1 has 21=22^{1} = 2 nodes, and level 2 has 22=42^{2} = 4 nodes. In general, level kk has 2k2^{k} nodes. Every new level doubles the count of the level above it, which is why the total grows so fast.

Why is the sum one less than a power of two? Adding every level from 0 to hh gives 1+2+4+⋯+2h1 + 2 + 4 + \cdots + 2^{h}. Each term is double the one before it, and a list like this always adds up to one less than the next power of two. Check with h=2h = 2: 1+2+4=71 + 2 + 4 = 7, and 23−1=72^{3} - 1 = 7. So the total is 2h+1−12^{h+1} - 1. The +1+1 in the exponent comes from the h+1h + 1 levels, and the −1-1 comes from this sum.

How big does it get? The count grows fast, because each new level doubles the bottom row. A perfect binary tree of height 20 has 221−12^{21} - 1 nodes, which is 2,097,151 nodes. Each extra level of height roughly doubles the total.

An everyday picture

Think of a knockout tournament bracket with eight team slots. The final is the root, two semifinals sit below it, four quarterfinals below those, and eight team slots form the bottom row. Each game is an inner node, and each team slot is a leaf. There are 7 games, and the 8 team slots plus the 7 games make 15 nodes, which is exactly what 23+1−12^{3+1} - 1 gives.

Worked example: A knockout bracket with 8 team slots

A knockout tournament with 8 team slots is drawn as a perfect binary tree. The final is the root, and each team slot is a leaf at the bottom. The tree’s height is h=3h = 3: going from the final down to a team slot takes 3 lines. The example uses simple counts, so each row can be counted by hand.

  1. Read the height. The final is the root at the top, and a team slot is a leaf at the bottom. Going from the final down to any team takes 3 lines, so the height is h=3h = 3. That gives four rows, numbered 0 to 3, with row 3 holding the team slots.
  2. Count the nodes on each row. Row 0 holds 20=12^{0} = 1 node, the final. Row 1 holds 21=22^{1} = 2 semifinals. Row 2 holds 22=42^{2} = 4 quarterfinals. Row 3 holds 23=82^{3} = 8 team slots. Each row has double the count of the row above it, because each game has two slots feeding into it.
  3. Add the rows. 1+2+4+8=151 + 2 + 4 + 8 = 15 nodes in total. The 8 team slots are the leaves on the bottom row. The other nodes, 1 + 2 + 4 = 7, are games, because a game is an inner node that joins two slots.
  4. Use the formula in one step. N=2h+1−1=23+1−1N = 2^{h+1} - 1 = 2^{3+1} - 1. The exponent is 3+1=43 + 1 = 4, and 24=2×2×2×2=162^{4} = 2 \times 2 \times 2 \times 2 = 16. Subtracting 1 gives 16−1=1516 - 1 = 15 nodes, the same as the row total from the previous step.
  5. Count the games. There are 15 nodes in all, and 8 of them are team slots, so the games are 15−8=715 - 8 = 7. This matches a general rule for knockouts: a bracket with nn teams has n−1n - 1 games, here 8−1=78 - 1 = 7.
  6. Check the bottom row. The bottom row is 2h=23=2×2×2=82^{h} = 2^{3} = 2 \times 2 \times 2 = 8. Eight team slots fill it with no gaps, which is what makes the tree perfect. If a slot were missing, the tree would have fewer nodes than the formula gives.
  7. Say it plainly. An 8-team knockout has 15 nodes: 8 team slots and 7 games. The formula found that total from the height alone, so there is no need to count every row, although counting the rows is a good way to check the answer.

Answer: The tree has 15 nodes in total: 8 team slots on the bottom row and 7 games above them. The formula gives the total from the height alone, 23+1−1=152^{3+1} - 1 = 15.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Is a perfect binary tree the same as a complete binary tree?

No. A perfect tree has every row full. A complete tree fills every row except possibly the last, and the last row fills from the left with no gaps. Every perfect tree is complete, but not every complete tree is perfect.

How many leaves does a perfect binary tree have?

2h2^{h} leaves for height hh, because each row doubles the one above it. Height 3 gives 23=82^{3} = 8 leaves, and height 10 gives 210=10242^{10} = 1024 leaves.

Why is there a +1 in the exponent?

A tree of height hh has h+1h + 1 rows, because height counts edges and the rows are numbered from 0 to hh. The sum of all the rows is 2h+1−12^{h+1} - 1, so the exponent must be one bigger than the height.

Sources

  1. Binary tree, Wikipedia
  2. Tree (data structure), Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/perfect-binary-tree-nodes.md.