Trees
Each problem is a recursion with the empty tree as base case, or a level-order loop with a queue. Say which before you start.
The lesson is Trees.
Count the nodes
Write count(root) that returns how many nodes a binary tree has. An empty tree has 0.
count([8, 3, 10, 1, 6, None, 14]) -> 6
count(None) -> 0
The point is to practise the basic tree recursion.
def count(root):
...
Largest value in a tree
Write largest(root) that returns the largest value in a non-empty binary tree. The tree
is not necessarily a search tree.
largest([8, 3, 10, 1, 6, None, 14]) -> 14
largest([5]) -> 5
The point is to practise a recursion that combines with max.
def largest(root):
...
Shortest path to a leaf
Write min_depth(root) that returns the number of nodes on the shortest path from the
root to any leaf. An empty tree has depth 0. Note a node with one child is not a leaf.
min_depth([3, 9, 20, None, None, 15, 7]) -> 2
min_depth([2, None, 3, None, 4]) -> 3
min_depth(None) -> 0
The point is to practise a recursion where a missing child must not be counted as a path.
def min_depth(root):
...
Are two trees the same?
Write same(a, b) that returns whether two binary trees have the same shape and the
same values in the same places.
same([1, 2, 3], [1, 2, 3]) -> True
same([1, 2], [1, None, 2]) -> False
same(None, None) -> True
The point is to practise recursing over two trees in step.
def same(a, b):
...
Is the tree balanced?
A binary tree is height-balanced if, at every node, the heights of the left and right
subtrees differ by at most one. Write is_balanced(root).
is_balanced([3, 9, 20, None, None, 15, 7]) -> True
is_balanced([1, 2, 2, 3, 3, None, None, 4, 4]) -> False
is_balanced(None) -> True
The point is to practise a recursion that returns extra information upward so each node is visited once: return the height, or a sentinel meaning "unbalanced below".
def is_balanced(root):
...
Is there a path with this sum?
Write has_path_sum(root, target) that returns whether some path from the root to a leaf
has values adding up to target. An empty tree has no paths.
has_path_sum([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1], 22) -> True (5+4+11+2)
has_path_sum([1, 2, 3], 5) -> False
has_path_sum(None, 0) -> False
The point is to practise passing a shrinking target down the recursion and recognising leaves.
def has_path_sum(root, target):
...
In-order values
Write inorder(root) that returns the values of a binary tree in in-order: left subtree,
node, right subtree. On a binary search tree this is sorted order.
inorder([8, 3, 10, 1, 6, None, 14]) -> [1, 3, 6, 8, 10, 14]
inorder(None) -> []
The point is to practise a traversal that collects into a list, recursively or with an explicit stack.
def inorder(root):
...
Is it a valid search tree?
Write is_bst(root) that returns whether the tree satisfies the binary search tree rule
everywhere: every value in a node's left subtree is strictly less than the node, every
value in its right subtree strictly greater.
is_bst([2, 1, 3]) -> True
is_bst([5, 1, 4, None, None, 3, 6]) -> False (3 is in the right subtree of 5)
is_bst(None) -> True
The point is to practise passing allowed bounds down the recursion. Checking each node against only its children misses the second example.
def is_bst(root):
...
Lowest common ancestor in a search tree
root is a binary search tree containing both a and b. Write lca(root, a, b) that
returns the value of their lowest common ancestor: the deepest node that has both among
its descendants (a node counts as its own descendant).
lca([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 2, 8) -> 6
lca([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 2, 4) -> 2
lca([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 3, 5) -> 4
The point is to practise using the BST rule to walk down without searching both sides.
def lca(root, a, b):
...
Seen from the right
Write right_view(root) that returns the values you would see looking at the tree from
the right: the last node on each level, top to bottom.
right_view([1, 2, 3, None, 5, None, 4]) -> [1, 3, 4]
right_view([1, 2]) -> [1, 2]
right_view(None) -> []
The point is to practise level-order traversal where only one node per level is kept.
def right_view(root):
...
k-th smallest in a search tree
Write kth_smallest(root, k) that returns the k-th smallest value in a binary search
tree, with k = 1 the smallest. k is at most the number of nodes.
kth_smallest([3, 1, 4, None, 2], 1) -> 1
kth_smallest([5, 3, 6, 2, 4, None, None, 1], 3) -> 3
The point is to practise stopping an in-order traversal early.
def kth_smallest(root, k):
...
Next: Graphs.