This is O(n) , or rather, Theta(n) .
Look at each node in the tree - each node is βvisitedβ no more than 3 times and at least once) - when it is detected (all nodes), when returning from the left son (non leaf), and when returning from the right son (non leaf), so that the total number of 3 * n visits is the largest and n visits, at least on node. Each visit is O(1) (queue push / pop), the summation is in Theta(n) .
source share