Implement a Hierarchical Class-Based DOM Selector with Ancestor Matching

Build a function getByClassNameHierarchy(root, classPath) that accepts a root DOM node and a space-separated sequence of class names representing a parent-to-child hierarchy (e.g., 'container list item'), and returns all elements that match the leaf class AND have the preceding ancestors in order. This is essentially how CSS descendant selectors work internally — a step above a flat class search and requires multi-level tree traversal with ancestor validation.

Examples
Input: []
Output:
Hints

Implement a Hierarchical Class-Based DOM Selector with Ancestor Matching

Build a function getByClassNameHierarchy(root, classPath) that accepts a root DOM node and a space-separated sequence of class names representing a parent-to-child hierarchy (e.g., 'container list item'), and returns all elements that match the leaf class AND have the preceding ancestors in order. This is essentially how CSS descendant selectors work internally — a step above a flat class search and requires multi-level tree traversal with ancestor validation.