Skip to content

Commit d4d4620

Browse files
My solution to 1367
1 parent 13b6472 commit d4d4620

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

problems/1367/jeremymanning.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
# [Problem 1367: Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4+
- First we need to find all nodes in the tree whose values match the head of the linked list's value. These are "candidate starts of paths."
5+
- Then, for each candidate:
6+
- Start with the head of the linked list, and the candidate node. If we ever reach a point where the linked list node's `next` value is `None`, return `True`.
7+
- Otherwise, if any of the children of the current binary tree node have the same value as the current linked list's next node, then move along in the linked list to the next node, and move along in the tree to the corresponding child.
8+
- Tricky special case: if *both* children match, then we need to enqueue *both* options. So actually, maybe the queue of possible paths should include touples of the place to start in the linked list and the place to start in the binary tree. That way we can just keep adding possibilities until they're all exhausted. (Or...maybe we should add options in a stack instead of a queue, since that way shorter paths will appear near the end and can be more quickly eliminated?)
9+
- If *neither* of the current binary tree node's children match the next linked list node, continue on to the next path possibility
10+
- If we ever run out of options (stack is empty), return `False`
411

512
## Refining the problem, round 2 thoughts
13+
- I think this is a straightforward approach. We can start with a breadth-first search to seed the stack.
614

715
## Attempted solution(s)
816
```python
9-
class Solution: # paste your code here!
10-
...
17+
from collections import deque
18+
# Definition for singly-linked list.
19+
# class ListNode:
20+
# def __init__(self, val=0, next=None):
21+
# self.val = val
22+
# self.next = next
23+
# Definition for a binary tree node.
24+
# class TreeNode:
25+
# def __init__(self, val=0, left=None, right=None):
26+
# self.val = val
27+
# self.left = left
28+
# self.right = right
29+
class Solution:
30+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
31+
# first find all possible starts of the tree path, using BFS of the tree
32+
options = []
33+
queue = deque([root])
34+
while queue:
35+
node = queue.popleft()
36+
if node is None:
37+
continue
38+
if node.val == head.val:
39+
options.append((node, head))
40+
queue.extend([node.left, node.right])
41+
42+
# now use DFS of all possible options
43+
while options:
44+
next_root, next_head = options.pop()
45+
if next_head.next is None:
46+
return True
47+
if next_root.left is not None and next_root.left.val == next_head.next.val:
48+
options.append((next_root.left, next_head.next))
49+
if next_root.right is not None and next_root.right.val == next_head.next.val:
50+
options.append((next_root.right, next_head.next))
51+
52+
return False
1153
```
54+
- Given test cases pass
55+
- I'm in a rush tonight, so I'm just going to submit!
56+
57+
![Screenshot 2024-09-06 at 10 02 01 PM](https://github.com/user-attachments/assets/90897c63-7c1d-4d7b-ab6a-fb98000c62b2)
58+
59+
Great, it worked!
60+

0 commit comments

Comments
 (0)