Linked List in Binary Tree - Complete Solution Guide
Linked List in Binary Tree is LeetCode problem 1367, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Example 1: 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 Explanation: Nodes in blue form a subpath in the binary Tree. Example
Detailed Explanation
The problem asks us to determine whether a linked list's sequence of values exists as a downward path within a binary tree. A downward path means a sequence of connected nodes starting from any node in the tree and going down towards its descendants (children). We need to return `True` if such a path exists, and `False` otherwise. The linked list represents the target path, and the binary tree represents the search space. The values in the linked list and the tree nodes can range from 1 to 100. The size of the tree is up to 2500 nodes and the linked list can have up to 100 nodes.
Solution Approach
The provided code uses a dynamic programming approach to solve this problem. It first extracts the values from the linked list into a list called `list_vals`. Then, it performs a breadth-first search (BFS) on the binary tree to get a list of all tree nodes called `all_tree_nodes` and a map from each node to its index in the `all_tree_nodes` list called `node_to_idx`. The core of the algorithm is a dynamic programming table `dp` where `dp[i]` is true if the *last* value of `list_vals` can be found at the `i`th tree node in `all_tree_nodes`. The code iterates backward through the `list_vals`, using the `dp` table from the previous iteration to determine the values of `new_dp`. If `new_dp[j]` is true, this implies that a subpath starting at the node `all_tree_nodes[j]` can lead to the end of the linked list sequence. The backward iteration continues, and finally, if any entry in the `dp` table is true, it means there exists a subpath in the tree matching the linked list.
Step-by-Step Algorithm
- Step 1: Convert the linked list into a list of integers `list_vals` for easier access.
- Step 2: Traverse the binary tree using BFS and store all tree nodes in a list `all_tree_nodes`.
- Step 3: Create a hash map `node_to_idx` to map each tree node to its index in the `all_tree_nodes` list.
- Step 4: Initialize a DP table `dp` of size N (number of tree nodes) where `dp[j]` is true if the last element of the linked list `list_vals` is found at node `all_tree_nodes[j]`.
- Step 5: Iterate backward from the second to last element of `list_vals` to the first, updating the DP table at each step.
- Step 6: In each inner loop iteration, check if the current tree node's value matches the current element in `list_vals`. If it does, check if either its left or right child (if they exist) has a value of `True` in the previous DP table.
- Step 7: After the outer loop finishes, check if any element in the DP table is `True`. If so, the linked list is a subpath of the binary tree. Return `True` if a subpath exists, `False` otherwise.
Key Insights
- Insight 1: We can traverse the binary tree and for each node, check if the linked list is a subpath starting at that node.
- Insight 2: A dynamic programming approach can be used to efficiently determine if a subpath exists. By working backwards from the end of the linked list, we can build up a table indicating whether a portion of the linked list exists starting at a given node in the tree.
- Insight 3: Instead of recursively calling the subpath check function on each node, a more efficient approach pre-processes the tree into a list of all nodes and creates a map from node to index to avoid redundant traversals when using DP.
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(N)
Topics
This problem involves: Linked List, Tree, Depth-First Search, Binary Tree.
Companies
Asked at: SoundHound.