Back to problems

Merge Two Ordered Trees with Duplicate Child Names

Algorithm · Google · Medium

You receive two trees, t1 and t2. Every node in either tree carries three fields: name (a string), value (an integer), and children (an ordered list of child nodes). Combine the two trees into a brand-new tree and return it, following the rules below. Whenever two nodes are merged, the resulting node copies its name from the t1 node and its value from the t2 node. To merge the direct children of such a node, walk through t1.children in order from left to right. Pair each one…

Loading the rest of the question…