Linked List in Binary Tree
class Solution:
def isSubPath(self, head, root) -> bool:
@lru_cache(maxsize=None)
def rec(node, llnode, lhead):
if not llnode:
return True
elif not node:
return False
elif node.val != llnode.val:
return rec(node.left, lhead, lhead) or rec(node.right, lhead, lhead)
else:
sp1 = rec(node.left, llnode.next, lhead)
sp2 = rec(node.right, llnode.next, lhead)
sp3 = rec(node.left, lhead, lhead)
sp4 = rec(node.right, lhead, lhead)
if node.val == llnode.val:
return sp1 or sp2 or sp3 or sp4
else:
return sp3 or sp4
return rec(root, head, head)