DSA: Binary Trees
Saturday, 5 September 2026
Learning about Binary Trees
A binary tree is a tree in which each node has at most two children. The two child positions are distinct: one is the left child and the other is the right child. A node may have no children, one child, or two children.
Some properties of binary tree are:
- Max nodes at level
l: at most2^lnodes. - Max nodes at height
h: at most2^(h+1) − 1nodes (all levels completely filled). - Min height for
Nnodes:⌊log₂ N⌋. - Min levels for
Lleaves:⌈log₂ L⌉. - Full binary tree: leaves
L = T + 1, whereT= internal nodes with two children. - Total edges:
n − 1edges for a tree withnnodes.
Types of Binary Trees
Full Binary Tree
A full binary tree is a binary tree in which every node has either zero children or exactly two children. No node is allowed to have only one child.
1
/
2 3
/ /
4 5 6 7 Nodes 1, 2, and 3 each have two children. Nodes 4, 5, 6, and 7 have none, so this is full.
Perfect Binary Tree
A perfect binary tree is a full binary tree in which all leaf nodes are at the same level. Every level is completely filled.
1
/
2 3
/ /
4 5 6 7 For a perfect binary tree with height h measured in edges:
number of nodes = 2^(h + 1) - 1
number of leaves = 2^h Complete Binary Tree
A complete binary tree has every level fully filled except possibly its last level. Its final level must be filled from left to right, with no gaps between nodes.
1
/
2 3
/ /
4 5 6 This is not complete. There is a gap where 2’s right child should be, but a node appears later on the same level.
Balanced Binary Tree
A balanced binary tree keeps its height relatively small, usually O(log n), so operations do not degrade into linked-list-like walks. The exact rule depends on the kind of balanced tree.
For the common height-balanced definition, every node satisfies:
abs(height(left subtree) - height(right subtree)) <= 1 1
/
2 3
/
4 5 6 Degenerate or Skewed Binary Tree
A degenerate binary tree is one in which every parent has only one child. It behaves like a linked list. It may be left-skewed or right-skewed.
left-skewed: right-skewed:
1 1
/
2 2
/
3 3
/
4 4 For n nodes, a skewed tree has height n - 1, so DFS uses O(n) stack space. A Binary Search Tree with values inserted in sorted order can become skewed unless it is self-balancing.
Leetcode problems
- 2236. Root Equals Sum of Children (Easy)
- 572. Subtree of Another Tree (Easy)
- 951. Flip Equivalent Binary Trees (Medium)
- 965. Univalued Binary Tree (Easy)
- 222. Count Complete Tree Nodes (Easy)
- 129. Sum Root to Leaf Numbers (Medium)
- 988. Smallest String Starting From Leaf (Medium)
- 617. Merge Two Binary Trees (Easy)
- 814. Binary Tree Pruning (Medium)
- 2331. Evaluate Boolean Binary Tree (Easy)
- 1676. Lowest Common Ancestor of a Binary Tree IV (Medium)
- 1026. Maximum Difference Between Node and Ancestor (Medium)
- 662. Maximum Width of Binary Tree (Medium)
- 958. Check Completeness of a Binary Tree (Medium)
- 1028. Recover a Tree From Preorder Traversal (Hard)