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 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.

Visualization

Tap to expand
Linked List in Binary Tree - Path Matching144212618Target Sequence428Linked List: [4,2,8]โœ“ Match Found!Downward path exists:4 โ†’ 2 โ†’ 8Starting from left subtreeAlgorithm Steps1. DFS traverse all tree nodes2. For each node, try path matching3. Follow children for sequence4. Return true if complete matchRed highlighted nodes show the successful path: 4โ†’2โ†’8 exists as a downward sequence
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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(N ร— min(L,H))

N is the number of tree nodes, L is linked list length, H is tree height. For each node we potentially traverse down min(L,H) levels

n
2n
โœ“ Linear Growth
Space Complexity
O(H)

Recursion stack depth is at most the height of the tree

n
2n
โœ“ Linear Space

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
Asked in
Amazon 42 Microsoft 28 Google 21 Meta 15
73.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen