Max Adjusted Path Cost in Binary Tree
At Livspace, optimizing the design and assembly process for modular furniture is crucial for efficiency and customer satisfaction. Each design project can be broken down into a hierarchy of modules, represented as a binary tree. To better manage costs and resource allocation, we need to analyze the "projected cost" of different design paths. Some modules are designated as "critical" due to their significant impact or specialized requirements, and their costs are treated differently.
Your task is to implement a function that calculates the maximum possible "adjusted total cost" among all root-to-leaf paths in a given binary tree.
val (cost) is present in the criticalValues list, its contribution to the path sum is doubled.val normally.Input:
root: A TreeNode object representing the root of the binary tree. Each TreeNode has a val (number) and left, right pointers (TreeNode | null). The val represents the module's base cost.criticalValues: An array of numbers, where each number represents a val that, if encountered in a node, signifies a critical module.Output:
root is null), return 0.root is null, the function should return 0.criticalValues List: The criticalValues list can be empty. If so, no module costs will be doubled. Values in criticalValues are guaranteed to be positive integers, as are node vals.TreeNode.val will be positive integers.Examples
Example 1
Input:
root = [10,5,20,1,null,7], criticalValues = [5]
Output:
37
Note:
Paths:
Example 2
Input:
root = [1,2,3,4,5], criticalValues = [1,3]
Output:
9
Note:
Paths:
Example 3
Input:
root = [100], criticalValues = [100]
Output:
200
Note:
The only path is 100. Since 100 is in criticalValues, its cost is doubled: 100 * 2 = 200.
Constraints
TreeNode.val will be between 1 and 10^5.criticalValues array will be between 0 and 10^4.criticalValues[i] will be between 1 and 10^5.criticalValues lookups?Loading editor...