In the Difference Between Maximum and Minimum Price Sum interview question, you are given an undirected tree where each node represents a city with a specific price. A "root" is chosen, and you calculate the price sum of all paths starting from this root to any other node. You need to find the maximum possible difference between the maximum path sum and the minimum path sum across all possible choices of the root node. In a tree, the minimum path sum for a root is always the price of the root node itself (the path from root to itself).
This "Hard" level problem is asked by companies like Media.net and Directi to evaluate a candidate's depth in Tree interview patterns and Dynamic Programming. It requires moving beyond a simple DFS to understanding how to re-root a tree or maintain multiple states (path with and without leaf) during a post-order traversal. It tests your ability to optimize what would be an naive solution into a linear one.
This problem is solved using Dynamic Programming on Trees (specifically the Re-rooting technique or Diameter-like logic). You need to track two values for each subtree: the maximum path sum starting from the current node to a leaf, and the maximum path sum starting from the current node but excluding the actual leaf node. By combining these values from different branches at each node, you can calculate the maximum difference globally.
Imagine a tree: Node 1 (price 10) connected to Node 2 (price 5) and Node 3 (price 2). If Node 1 is root:
Study the Re-rooting DP pattern. It is a sophisticated technique where you first do a bottom-up pass to collect subtree info, then a top-down pass to propagate info from the rest of the tree to each node. This is the key to solving most "find property for every root" tree problems in linear time.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Subtree of the Same Color | Medium | Solve | |
| Minimum Increments to Equalize Leaf Paths | Medium | Solve | |
| Count Number of Possible Root Nodes | Hard | Solve | |
| Maximize Sum of Weights after Edge Removals | Hard | Solve | |
| Choose Edges to Maximize Score in a Tree | Medium | Solve |