Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 50s1 days 16 hours 1 minutes 50 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 50s1 days 16 hours 1 minutes 50 seconds.

Sign in to watch this lesson
Algorithms - Trees and Range Queries · 4:15
A tree is a connected graph without cycles: n - 1 edges and exactly one path between any two vertices. We pick up a 10-vertex tree by vertex 1 to root it, fill parent and depth with a dfs(v, p) that skips its parent instead of keeping a visited array, and let subtree sizes bubble up from the leaves. A path of 200,000 vertices crashes the recursive version on this Mac's 8 MB stack, so we redo it with a BFS order walked backwards, which handles a million vertices in milliseconds.