Coding Challenge Practice – Question 54

The task is to return the next sibling, given a DOM tree and a target element.

The boilerplate code

function nextRightSibling(root, target) {
// your code here
}

If there is no next sibling, return null

If(!root || !target) return null;


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to return the next sibling, given a DOM tree and a target element.

The boilerplate code

function nextRightSibling(root, target) {
  // your code here
}

If there is no next sibling, return null

If(!root || !target) return null;

Process the DOM tree level by level. Keep track of the level of the target element.

const queue = [[root, 0]];
  let targetDepth = null;

  while (queue.length) {
    const [node, depth] = queue.shift();

    if (node === target) {
      targetDepth = depth;
      continue;
    }

After the target is found, the next element on the same level as the target is the right sibling. If the next element is on a lower depth, the target has no right sibling

if (targetDepth !== null) {
      if (depth === targetDepth) {
        return node;
      } else if (depth > targetDepth) {
        return null;
      }
    }

The final code

function nextRightSibling(root, target) {
  // your code here
  if(!root || !target) return null;

  const queue = [[root, 0]];
  let targetDepth = null;

  while(queue.length) {
    const [node, depth] = queue.shift();

    if(node === target) {
      targetDepth = depth;
      continue;
    } 
    if(targetDepth !== null) {
      if(depth === targetDepth) {
        return node;
      } else if( depth > targetDepth) {
        return null;
      }
    }
    for(let child of node.children) {
      queue.push([child, depth + 1]);
    }
  }
  return null
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-11-13T22:43:45+00:00) Coding Challenge Practice – Question 54. Retrieved from https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/

MLA
" » Coding Challenge Practice – Question 54." Bukunmi Odugbesan | Sciencx - Thursday November 13, 2025, https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/
HARVARD
Bukunmi Odugbesan | Sciencx Thursday November 13, 2025 » Coding Challenge Practice – Question 54., viewed ,<https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 54. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/
CHICAGO
" » Coding Challenge Practice – Question 54." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/
IEEE
" » Coding Challenge Practice – Question 54." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 54 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/11/13/coding-challenge-practice-question-54/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.