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

Sign in to watch this lesson
Advanced - Trees · 4:47
One depth-first search stamps each vertex with tin, the moment it is entered; laid out in that order, every subtree becomes the contiguous range [tin, tout] with tout = tin + size - 1. Subtree sums then become range sums in a Fenwick tree over positions, subtree raises become range updates read back as prefix sums, and the two stamps answer 'is u an ancestor of v?' in O(1).