A perfect binary tree is a type of binary tree in which all interior nodes have exactly two children and all leaf nodes are at the same level. Additionally, a perfect binary tree must have all levels completely filled with nodes.
Key characteristics of a perfect binary tree:
- All interior nodes have exactly two children (either both left and right children or none).
- All leaf nodes are at the same level.
- All levels of the tree are completely filled with nodes.
Here's an example of a perfect binary tree with height 2:
1
/ \
2 3
/ \ / \
4 5 6 7
In this example:
- Node 1 is the root.
- Nodes 2 and 3 are the children of node 1.
- Nodes 4, 5, 6, and 7 are the children of nodes 2 and 3.
- All leaf nodes (nodes 4, 5, 6, and 7) are at the same level.
Perfect binary trees are less common in practice compared to complete or balanced binary trees, but they are conceptually important in understanding binary tree properties and algorithms.