Traverse DOM tree

You are required to implement a function that traverses the DOM tree and returns an array of all the elements in the tree.

Constraints

  • The input root is guaranteed to be a valid DOM element.
  • The tree can have any number of nested elements.

Example Input

<div id="root">
    <div id="child1">
        <span id="child1_1"></span>
    </div>
    <div id="child2">
        <span id="child2_1"></span>
        <span id="child2_2"></span>
    </div>
</div>

<script>
    const rootElement = document.getElementById('root');
    console.log(getAllElements(rootElement));
</script>

Output

[
    <div id="root">...</div>,
    <div id="child1">...</div>,
    <span id="child1_1"></span>,
    <div id="child2">...</div>,
    <span id="child2_1"></span>,
    <span id="child2_2"></span>
]