Linked List in Binary Tree - Problem
Linked List in Binary Tree
You're given two data structures: a binary tree with root node and a linked list starting from the head node. Your task is to determine if there exists a downward path in the binary tree that matches the exact sequence of values in the linked list.
A downward path means you start at any node in the binary tree and follow parent-to-child connections going downwards. The path doesn't need to start from the root - it can begin at any node and go down to any descendant.
Goal: Return
Example: If your linked list is
You're given two data structures: a binary tree with root node and a linked list starting from the head node. Your task is to determine if there exists a downward path in the binary tree that matches the exact sequence of values in the linked list.
A downward path means you start at any node in the binary tree and follow parent-to-child connections going downwards. The path doesn't need to start from the root - it can begin at any node and go down to any descendant.
Goal: Return
True if you can find such a matching downward path, False otherwise.Example: If your linked list is
[4,2,8] and your binary tree contains a path where some node has value 4, its child has value 2, and that child's child has value 8, then return True. Input & Output
example_1.py โ Basic Match Found
$
Input:
head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
โบ
Output:
true
๐ก Note:
The linked list [4,2,8] can be found as a downward path in the binary tree starting from the node with value 4 (left child of root), then its left child 2, then its right child 8.
example_2.py โ No Match Found
$
Input:
head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
โบ
Output:
false
๐ก Note:
Although all individual values [1,4,2,6] exist in the tree, there is no downward path that contains this exact sequence. The paths get broken - we can't find 6 as a child of any node with value 2 that follows the pattern.
example_3.py โ Single Node Match
$
Input:
head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
โบ
Output:
false
๐ก Note:
The sequence [1,4,2,6,8] is too long. While we can match [1,4,2,6] starting from root, we cannot extend this path further to include 8 as the next child, since 6 doesn't have 8 as a child in the required downward path.
Constraints
- The number of nodes in the tree is in the range [1, 2500]
- The number of nodes in the list is in the range [1, 100]
- 1 โค Node.val โค 100
- Both tree and linked list nodes can have duplicate values
- Tree nodes and list nodes are guaranteed to be integers
Visualization
Tap to expand
Understanding the Visualization
1
Find Starting Points
Visit each node in the binary tree as a potential starting point for our sequence
2
Validate Downward Path
From each starting point, check if we can follow children that match our linked list sequence
3
Match Complete Sequence
Ensure the entire linked list can be matched by following one continuous downward path
4
Return Success
Return true as soon as we find any valid path, or false if no path exists
Key Takeaway
๐ฏ Key Insight: Separate tree exploration from path validation - use DFS to find all potential starting nodes, then use a helper function to verify if the complete linked list sequence can be matched by following a single downward path.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code