distance

fun <T> TreeNode<T>.distance(other: TreeNode<T>): Int?(source)

The number of edges on the shortest path between this node and other.

Computed as depth() + other.depth() - 2 * lca.depth(), where lca is their lowestCommonAncestor. The distance from a node to itself is 0.

Return

the edge count, or null when the two nodes belong to different trees.

Runs in O(da + db) time, where da/db are the depths of the two nodes.

Parameters

other

the other node to measure the distance to.