Preview:
function isSubPath(head: ListNode | null, root: TreeNode | null): boolean {
    if (!head) return true;
    if (!root) return false;
    const dfs = (head: ListNode | null, root: TreeNode | null): boolean => {
        if (!head) return true; 
        if (!root) return false;
        if (root.val === head.val)
            return dfs(head.next, root.left) || dfs(head.next, root.right);
        return false;
    };
    return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter