Data Structures19 sections · 729 units
Open in Course

Why Rerooting Works

The reversal trick

In the auxiliary tree, left = toward root, right = away from root. When you want v to become root, the path from v to old root needs to reverse direction.

What was "toward root" becomes "away from root." Reversing the auxiliary tree (swapping all left/right children) achieves this:

  • Old: left children are ancestors
  • New: right children are ancestors (which are now descendants of v)

The lazy reversal flag avoids O(n)O(n) work.

You only push down when you traverse. This makes Link-Cut trees work with unrooted trees: any node can become the root temporarily.