Loading problem…
Given two structurally identical DOM trees (rootA and rootB) and a target element located somewhere inside tree A, find and return the exact corresponding DOM node in tree B.
In real-world usage, this problem maps directly to "virtual DOM" diffing scenarios: if you know a node changed in the previous tree representation, you must locate the exact same node position in the new tree to execute pinpoint UI updates.
Implement the findCorrespondingNode function:
function findCorrespondingNode(rootA, rootB, target) {}rootB that shares the exact same relative structural path from the root as target shares with rootA.target is identical to rootA, the answer is obviously rootB.null, immediately return null.target does not belong anywhere within the rootA sub-tree (an "outsider" node), safely return null instead of throwing an error or looping infinitely.// Tree A: <div><p>Sibling</p><span>Target</span></div>
// Tree B: <div><p>Sibling</p><span>Twin</span></div>
const rootA = document.getElementById('a');
const rootB = document.getElementById('b');
const target = rootA.children[1]; // The <span>
const twin = findCorrespondingNode(rootA, rootB, target);
// Result: The <span> element inside Tree B