AlgoPath Python · Book 4

Python A Level · Part 1Recursion, Trees and BFS

Trace recursive calls, explore trees and use breadth-first search to find routes through graphs and grids. Bring the ideas together in a tested route planner.

By AlgoPath Coding Academy
First edition, 2026 · 14 chapters

Python A Level · Part 1: Recursion, Trees and BFS — front cover

Who this book is for

Students moving beyond GCSE programming into recursive algorithms, trees and breadth-first search.

Before you begin

Write functions with return values, trace loops, use lists and dictionaries, and work with row and column indices. Start after Python GCSE Advanced or equivalent practice.

An independent programming resource, not a complete A level Computer Science specification. The further-study section extends the main chapters with additional tree tasks, Dijkstra's algorithm and disjoint sets.

Inside the book

The 14 chapters

  1. Understand and represent trees
  2. Trace recursive calls and returns
  3. Traverse trees and evaluate expressions
  4. Calculate results from subtrees
  5. Follow paths with recursion
  6. Search and update a binary search tree
  7. Explore graphs with a queue
  8. Find and reconstruct shortest paths
  9. Solve mazes and move across grids
  10. Find regions and use several starting points
  11. Search states rather than places
  12. Order tasks with dependencies
  13. Find routes with zero-or-one costs
  14. Build and test a route planner

Work through explanations and examples, try the independent practice, then use the explained answers to review your reasoning.

Try the approach

Trace a recursive return

For a non-negative integer, this function adds the values from that number down to zero. Keep track of where each call resumes.

def total_to(number):
    if number == 0:
        return 0
    return number + total_to(number - 1)

print(total_to(3))

What it prints

6

The base case returns 0. The waiting calls then return 1, 3 and finally 6. Each call has its own number; returning from a call resumes its caller.

This short public example introduces an idea explored in the book.