Source: https://bigfrontend.dev/problem/previous-left-sibling

// Can do recursive approach except check the previousElementSibling and lastElementChild
function previousLeftSibling(root, target) {
// If target is null, there is no previous left sibling
if (!target) {
return null;
}
// Check target's immediate previousElementSibling to see if there is a previous sibling on the same level
if (target.previousElementSibling) {
return target.previousElementSibling;
}
let parent = target.parentElement;
while (parent) {
parent = previousLeftSibling(root, parent);
if (parent && parent.lastElementChild) {
return parent.lastElementChild;
}
}
return null;
}