Longest Path With Different Adjacent Characters - Complete Solution Guide
Longest Path With Different Adjacent Characters is LeetCode problem 2246, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1 . The tree is represented by a 0-indexed array parent of size n , where parent[i] is the parent of node i . Since node 0 is the root, parent[0] == -1 . You are also given a string s of length n , where s[i] is the character assigned to node i . Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same ch
Detailed Explanation
The problem asks us to find the longest path in a tree where no two adjacent nodes have the same character. We are given a tree represented by a `parent` array, where `parent[i]` is the parent of node `i`, and a string `s` where `s[i]` is the character of node `i`. The goal is to return the length of the longest valid path.
Solution Approach
The solution uses Depth-First Search (DFS) to traverse the tree. For each node, we recursively find the longest path from each of its children. While traversing, we maintain two variables, `longest_1` and `longest_2`, which represent the lengths of the two longest paths from the children of the current node that have different characters than the current node. We update the global `max_len` variable with the maximum path length found so far. The length of the path passing through the current node is `1 + longest_1 + longest_2`.
Step-by-Step Algorithm
- Step 1: Build the adjacency list representation of the tree from the `parent` array.
- Step 2: Initialize `max_len` to 0. This variable will store the length of the longest valid path found so far.
- Step 3: Define a recursive DFS function that takes a node as input.
- Step 4: Inside the DFS function, initialize `longest_1` and `longest_2` to 0. These variables will store the lengths of the two longest paths from the children of the current node.
- Step 5: Iterate through the children of the current node.
- Step 6: For each child, recursively call the DFS function to find the longest path from that child.
- Step 7: If the character of the current node is different from the character of the child, update `longest_1` and `longest_2` accordingly. If the path length from the child is greater than `longest_1`, update `longest_2` to the previous value of `longest_1` and update `longest_1` to the path length from the child. Otherwise, if the path length from the child is greater than `longest_2`, update `longest_2` to the path length from the child.
- Step 8: Update `max_len` with the maximum of its current value and `1 + longest_1 + longest_2`. The 1 accounts for the current node in the path.
- Step 9: Return `1 + longest_1` to the parent node. This represents the length of the longest path from the current node to one of its descendants.
- Step 10: Call the DFS function starting from the root node (node 0).
- Step 11: Return the value of `max_len`.
Key Insights
- Insight 1: The longest path doesn't necessarily have to pass through the root node (node 0).
- Insight 2: We can use Depth-First Search (DFS) to traverse the tree and compute the longest path from each node.
- Insight 3: For each node, we need to consider the longest two paths from its children that have different characters from the node itself. This is because these two paths can be combined to form a longer path passing through the current node.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, String, Tree, Depth-First Search, Graph, Topological Sort.
Companies
Asked at: Hudson River Trading, Target.