Leetcode Problem 2538. Difference Between Maximum and Minimum Price Sum
2538. Difference Between Maximum and Minimum Price Sum
Leetcode Solutions
Re rooting | O(N) clean code
Initialize an adjacency list to represent the graph.
Perform the first DFS to compute the maximum path sum for each node when it is the root.
Store the maximum path sum in a 2D array dp where dp[i][0] represents the maximum path sum including node i and dp[i][1] represents the second maximum path sum.
Perform the second DFS to update the dp array by considering re-rooting at each node.
During the second DFS, update the maximum path sum for the children by considering the maximum path sum from the parent node.
Keep track of the global maximum difference between the maximum and minimum path sums.
Return the global maximum difference as the result.