---
title: "Discussion on \"Find corresponding node in an identical DOM tree\" | Hashnode"
description: "Discussion on \"Find corresponding node in an identical DOM tree\". Given two identical DOM trees A and B with their root nodes rootA and rootB respectively, and a node nodeA in A, find the node nodeB at the corresponding po"
url: "https://hashnode.com/posts/find-corresponding-node/60cd6833554b0d31c60540b2"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Find corresponding node in an identical DOM tree](https://rajatexplains.dev/find-corresponding-node?utm_source=hashnode&utm_medium=feed)

Given two identical DOM trees A and B with their root nodes rootA and rootB respectively, and a node nodeA in A, find the node nodeB at the corresponding po

Jun 19, 2021 · 3 min read · rajatexplains.dev  
Read the article

-   [#html5](https://hashnode.com/tag/html5)
-   [#dom](https://hashnode.com/tag/dom)
-   [#interview](https://hashnode.com/tag/interview)
-   [#frontend-development](https://hashnode.com/tag/frontend-development)
-   [#html](https://hashnode.com/tag/html)

## Written by

[Rajat Jain](https://hashnode.com/@rajatexplains)

@rajatexplains · Bengaluru, India

  
[View profile](https://hashnode.com/@rajatexplains)

Software Engineer II 👨‍💻 at Uber | ex-Microsoft | Javascript ❤️ | Mentor 👨‍🏫 at CodeAsylums | Tech Speaker 🗣️ | React | GraphQL

I am a UI/UX enthusiast currently working as an engineer in Uber. Prior to this, I was at Microsoft for 4 years working on their most loved and complex product Microsoft Teams. I have a professional experience of training students in software engineering and computer science concepts and helping them land their dream jobs.

## Responses 2

Markdown supported · ⌘↵ to post

[Shivam Bhalla](https://hashnode.com/@shivambh28)

[Mar 5, 2023](https://hashnode.com/posts/find-corresponding-node/60cd6833554b0d31c60540b2/comment/6404ee4c87595bb6fc885a8a)

Very nice! I made a video about a simpler solution as well here on youtube

`youtube.com/watch?v=ElHeF2zY570&t=45s`

[Renan Couto](https://hashnode.com/@renancouto)

[Dec 23, 2021](https://hashnode.com/posts/find-corresponding-node/60cd6833554b0d31c60540b2/comment/61c40b042d88363ed4eaa3d4)

Hey Rajat, great post! The explanation and the image really helped to understand it.

I solved using your initial solution, but slightly differently:

```
function getPath(node, rootA) {
    const path = [];
    let currentNode = node;

    while (currentNode !== rootA) {
        const parent = currentNode.parentElement;
        const index = [...parent.children].indexOf(currentNode);
        path.unshift(index);
        currentNode = parent;
    }

    return path;
}

function getNodeFromPath(rootB, path) {
    return path.reduce((node, index) => node.children[index], rootB);
}
```

I got the path in the top-down order, that's why I used `unshift` instead of `push`, and then I reduced that path until its last item to find the correspondent node from root B.

Now, doing this recursively would be:

```
function findNode(nodeA, nodeB, targetNode) {
    for (let index = 0; index < nodeA.childElementCount; index++) {
        const currentNode = nodeA.children[index];

        if (currentNode === targetNode) {
            return nodeB.children[index];
        }

        if (currentNode.childElementCount) {
            return findNode(currentNode, nodeB.children[index], targetNode);
        }
    }
}
```

The idea is to go from the top until finding the node from root A. Once that is done, return the node from root B at the same position, otherwise, loop its children (if any) using the same function (recursively).

The recursive solution is probably more performant because:

-   it loops through the tree once, while the interactive solution has to go twice. First to find the path, then to find the node from the path itself
-   it doesn't create new arrays like the interactive one does, to get the path and also to find the index of the node from the children when getting the path

I'm not sure how to express that complexity in terms of the Big O notation, my guess is that the recursive solution is `O(n)` where `n` is the length of root A's tree. Maybe the interactive solution would be expressed in the same way, although we are looping it twice. Any ideas?

[Rajat Jain](https://hashnode.com/@rajatexplains)

[Jan 7, 2022](https://hashnode.com/posts/find-corresponding-node/60cd6833554b0d31c60540b2/comment/61c40b042d88363ed4eaa3d4#reply-61d8601b4b84101d2c3ee574)

Hi [Renan Couto](https://hashnode.com/@renancouto), great job explaining the recursive solution. I personally find the iterative solution for this problem quite intuitive during the interview, and that is why I went forward with it. However, recursive solution also has the same time complexity, as you mentioned it is **O(n)**. And yes, thank you for appreciating the post :)
