You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1, represented by a 2D array edges of length n - 1, where edges[i] = [ui, vi, lengthi] indicates an edge between nodes ui and vi with length lengthi. You are also given an integer array nums, where nums[i] represents the value at node i.
A special path is defined as a downward path from an ancestor node to a descendant node such that all the values of the nodes in that path are unique.
Note that a path may start and end at the same node.
Return an array result of size 2, where result[0] is the length of the longest special path, and result[1] is the minimum number of nodes in all possible longest special paths.
Example 1:
Input: edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]
Output: [6,2]
Explanation:
nums
The longest special paths are 2 -> 5 and 0 -> 1 -> 4, both having a length of 6. The minimum number of nodes across all longest special paths is 2.
Example 2:
Input: edges = [[1,0,8]], nums = [2,2]
Output: [0,1]
Explanation:

The longest special paths are 0 and 1, both having a length of 0. The minimum number of nodes across all longest special paths is 1.
Constraints:
2 <= n <= 5 * 104edges.length == n - 1edges[i].length == 30 <= ui, vi < n1 <= lengthi <= 103nums.length == n0 <= nums[i] <= 5 * 104edges represents a valid tree.Problem Overview: You are given a tree where each node has a value. A path is considered special if no value appears more than once along that path. The task is to compute the maximum length path that satisfies this constraint.
Approach 1: Brute Force DFS from Every Node (O(n^2) time, O(n) space)
Start a DFS from every node and explore all possible downward paths while tracking the values seen so far using a hash set. If the next node introduces a duplicate value, stop exploring that branch. During traversal, track the maximum path length encountered. This approach works because a tree has no cycles, but it redundantly explores many overlapping paths, leading to quadratic complexity in the worst case.
Approach 2: DFS with Hash Map + Prefix Distance (O(n) time, O(n) space)
The optimal approach performs a single DFS from the root while maintaining a sliding-window style constraint on the current path. Use a hash map to store the most recent depth where each value appeared. Also maintain prefix distances so you can compute the length of any valid segment quickly. When a duplicate value appears, shift the left boundary of the valid path to the position after the previous occurrence. This technique mirrors the classic "longest substring without repeating characters" pattern but applied along a tree traversal using Depth-First Search. Hash lookups ensure duplicates are detected in O(1).
During DFS, update the current window boundary based on the last occurrence of the node value. The prefix distance array allows constant-time computation of the path length between two depths. Backtracking restores the previous state of the hash map so other branches are evaluated independently. Combining Hash Table tracking with Tree traversal ensures each node is processed once.
Recommended for interviews: The brute force DFS demonstrates understanding of tree traversal and path validation but is rarely acceptable for large inputs. Interviewers expect the optimized DFS with a hash map and prefix distance tracking because it reduces repeated work and runs in linear time. Recognizing the similarity to the "longest unique subarray" sliding window pattern applied to a DFS path is the key insight.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS from Every Node | O(n^2) | O(n) | Useful for understanding the problem or when constraints are very small |
| DFS with Hash Map + Prefix Distance | O(n) | O(n) | Optimal solution for large trees where duplicate values must be tracked efficiently |
3425. Longest Special Path | Sliding Window on Trees | Crash Course | Backtracking | Sliding Window • Aryan Mittal • 4,765 views views
Watch 4 more video solutions →Practice Longest Special Path with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor