We consider a stochastic susceptible-infected-recovered (SIR) model with contact tracing on random trees and on the configuration model. On a rooted tree, where initially all individuals are susceptible apart from the root which is infected, we are able to find exact formulas for the distribution of the infectious period. Thereto, we show how to extend the existing theory for contact tracing in homogeneously mixing populations to trees. Based on these formulas, we discuss the influence of randomness in the tree and the basic reproduction number. We find the well known results for the homogeneously mixing case as a limit of the present model (tree-shaped contact graph). Furthermore, we develop approximate mean field equations for the dynamics on trees, and - using the message passing method - also for the configuration model. The interpretation and implications of the results are discussed.