Sobes.tech
Junior — Senior

Finding the nearest common ancestor in the screen hierarchy

livecode

Task condition

It is necessary to implement a helper function firstCommonParent that takes two screen nodes located in a common hierarchy and returns their nearest common ancestor. The function is used in the routing mechanism: first, a rollback (pop) occurs to the found ancestor, and then a transition (push) to the target screen.

class Screen(
    var parent: Screen? = null,
    var children: List<Screen> = emptyList()
)

fun firstCommonParent(nodeA: Screen, nodeB: Screen): Screen? {
    ...
}

Examples of calls:

  • firstCommonParent(C, H)A
  • firstCommonParent(C, G)D
  • firstCommonParent(H, D)D
  • firstCommonParent(G, F)A
  • firstCommonParent(C, L)null