Skip to content

Commit 62272a6

Browse files
Time: 118 ms (36.64%) | Memory: 18.9 MB (22.39%) - LeetSync
1 parent fa6c91b commit 62272a6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
class Solution:
13+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
14+
def helper(root, head):
15+
if not head:
16+
return True
17+
18+
if not root:
19+
return False
20+
21+
if root.val == head.val:
22+
return (helper(root.left, head.next) or helper(root.right, head.next))
23+
24+
if not head:
25+
return True
26+
27+
if not root:
28+
return False
29+
return helper(root, head) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)

0 commit comments

Comments
 (0)