Linked List in Binary Tree - Problem
Given a binary tree root and a linked list with head as the first node.
Return true if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return false.
In this context downward path means a path that starts at some node and goes downwards.
Input & Output
Example 1 — Path Found
$
Input:
root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3], head = [4,2,8]
›
Output:
true
💡 Note:
The path 4→2→8 exists in the tree starting from the left node with value 4, going right to 2, then right to 8
Example 2 — Path Not Found
$
Input:
root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3], head = [1,4,2,6]
›
Output:
false
💡 Note:
No downward path in the tree matches the sequence 1→4→2→6 from the linked list
Example 3 — Single Node Match
$
Input:
root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3], head = [1]
›
Output:
true
💡 Note:
Single node 1 exists in the tree, so the path is found
Constraints
- The number of nodes in the tree will be in the range [1, 2500]
- The number of nodes in the list will be in the range [1, 100]
- 1 ≤ Node.val ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Binary tree and linked list with values to match
2
Search
Try each tree node as potential starting point for linked list
3
Match
Check if downward path from node matches linked list sequence
Key Takeaway
🎯 Key Insight: The linked list can start from any tree node, not just the root - use DFS to check all possibilities
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code