Parse Nested HTML into a Tree
Problem statement
Parse a well-formed markup string into its nested element tree and return the element names in depth-first preorder.
The grammar contains paired lowercase tags such as <section>...</section> and arbitrary text leaves. Attributes, comments, entities, void elements, and self-closing tags are outside scope. The input has exactly one root element.
Function
preorderElementNames(html: String) → String[]Examples
Example 1
html = "<div>Hello<span>world</span></div>"return = ["div","span"]Preorder visits the root div before its nested span.
Example 2
html = "<a><b></b><c><d></d></c></a>"return = ["a","b","c","d"]Depth-first preorder follows a, b, c, then d.
Constraints
7 <= html.length <= 200000.- Tag names contain one to twenty lowercase ASCII letters.
- The markup is well formed and has one root element.