Watch 10 video solutions for Longest Path With Different Adjacent Characters, a hard level problem involving Array, String, Tree. This walkthrough by codestorywithMIK has 13,004 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 character assigned to them.
Example 1:
Input: parent = [-1,0,0,1,1,2], s = "abacbe" Output: 3 Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned. It can be proven that there is no longer path that satisfies the conditions.
Example 2:
Input: parent = [-1,0,0,0], s = "aabc" Output: 3 Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.
Constraints:
n == parent.length == s.length1 <= n <= 1050 <= parent[i] <= n - 1 for all i >= 1parent[0] == -1parent represents a valid tree.s consists of only lowercase English letters.Problem Overview: You are given a tree with n nodes where parent[i] represents the parent of node i. Each node has a character from string s. The goal is to find the length of the longest path such that every pair of adjacent nodes in the path has different characters.
Approach 1: DFS with Recursion (Tree DP) (Time: O(n), Space: O(n))
This approach treats the tree as a graph and performs a depth-first traversal starting from the root. For each node, compute the longest valid chain that extends downward through its children while ensuring adjacent characters differ. During DFS, track the two longest child paths whose characters are different from the current node. Combining these two paths through the current node forms a candidate for the global longest path.
The key insight: the answer may pass through a node by joining two valid child branches. Maintain a global maximum while DFS returns the best single branch length to its parent. This is essentially a tree dynamic programming pattern using Depth-First Search on a tree. Each edge is processed once, giving O(n) time and O(n) recursion stack space.
Approach 2: Iterative BFS with Topological Processing (Time: O(n), Space: O(n))
This method processes the tree from leaves upward using a queue, similar to topological ordering. First build an adjacency list and compute the number of children for each node. Start with leaf nodes and propagate their longest valid path lengths toward their parents. For every node, keep track of the two longest valid chains received from children whose characters differ from the parent.
When a node finishes processing all children, combine its two best chains to update the global answer. Then push the computed best chain upward to its parent. This avoids recursion and works well when stack depth could be large. The technique resembles a mix of graph traversal and topological sort over a tree structure.
Recommended for interviews: The DFS recursive solution is what most interviewers expect. It directly models the problem as tree dynamic programming and is easy to reason about once you track the two longest valid child paths. The BFS/topological approach demonstrates deeper understanding of tree processing without recursion, which can be useful when discussing scalability or iterative alternatives.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS with Recursion (Tree DP) | O(n) | O(n) | Best general solution. Clean logic for combining child paths in tree problems. |
| Iterative BFS / Topological Processing | O(n) | O(n) | Useful when avoiding recursion or handling very deep trees. |