Least Common Ancestor DOM

<div id="root">
  <div id="answer">
    <div>
      <div id="a">
        A
      </div>
    </div>
    <div>
      <div id="b">
        B
      </div>
    </div>
  </div>
</div>

<script>
  const leastCommonAncestor = (root, a, b, visited = {}) => {
    /*  if (root === null) return root;
     if (root === a) visited.v1 = true;
     if (root === b) visited.v2 = true;
     if (root === a || root === b) return root;
     const left = (root.left, a, b, visited);
     const right = (root.right, a, b, visited);
     if (left !== null  && right !== null && visited.v1 && visited.v2) {
         return root;
     }
     return left === null ? right : left; */
    let curr = a;
    while (curr !== root) {
      curr.dataset.visited = true;
      curr = curr.parentElement;
    }
    curr = b;
    while (curr !== root) {
      if (curr.dataset.visited === 'true') return curr;
      curr = curr.parentElement;
    }
    return null;
  };
  console.log(leastCommonAncestor(document.getElementById('root'), document.getElementById('a'), document.getElementById('b')).id);
/*
const findLCA = (root, a, b) => {
  if (root === null) return;
  if (root === a || root === b) return root;
  const ret = [];
  for (const child of [...root.children]) {
    const val = findLCA(child, a, b);
    if (val) {
      ret.push(val);
    }
  }
  if (ret.length === 2) return root;
  return ret[0];
};
*/
</script>


Demo