Binary Tree Maximum Path Sum - Complete Solution Guide
Binary Tree Maximum Path Sum is LeetCode problem 124, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once . Note that the path does not need to pass through the root. The path sum of a path is the sum of the node's values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path . Example 1: Input: root = [1,2,3] Output: 6 Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2
Detailed Explanation
The problem asks us to find the maximum path sum in a binary tree. A path is a sequence of nodes where adjacent nodes are connected by an edge. A node can appear in the sequence only once, and the path doesn't necessarily have to pass through the root. The path sum is the sum of the node values in the path, and we need to return the largest possible path sum among all possible paths. Constraints include the number of nodes being between 1 and 30,000, and node values being between -1000 and 1000.
Solution Approach
The solution uses a Depth-First Search (DFS) approach. For each node, we recursively calculate the maximum path sum in its left and right subtrees. The DFS function returns the maximum path sum that can be obtained by starting at that node and going down to one of its descendants (or stopping at the node itself). During the recursion, we also update a global `max_sum` variable with the maximum path sum that can be obtained by passing through the current node (forming a 'V' shape). Negative gains from the left or right subtrees are discarded by taking the maximum with 0.
Step-by-Step Algorithm
- Step 1: Initialize a global variable `max_sum` to negative infinity to keep track of the maximum path sum found so far.
- Step 2: Define a recursive function `dfs(node)` that takes a node as input.
- Step 3: Base case: If the node is null, return 0.
- Step 4: Recursively calculate the maximum gain from the left subtree (`left_gain`) and the right subtree (`right_gain`) by calling `dfs` on the left and right children, respectively. Take the maximum of the returned value and 0 to discard negative gains.
- Step 5: Calculate the maximum path sum that passes through the current node (`current_path_sum`) by adding the node's value, `left_gain`, and `right_gain`.
- Step 6: Update `max_sum` with the maximum of its current value and `current_path_sum`.
- Step 7: Return the maximum gain that a parent node can get by extending a path from the current node. This is the node's value plus the maximum of `left_gain` and `right_gain`.
- Step 8: Call `dfs(root)` to start the recursion from the root node.
- Step 9: Return the final `max_sum`.
Key Insights
- Insight 1: The maximum path sum can be a path between any two nodes in the tree, not necessarily passing through the root or any specific node.
- Insight 2: A path can either extend from a node to one of its children or form a 'V' shape through the node, utilizing both children. We need to consider both possibilities.
- Insight 3: Negative node values can reduce the path sum, so we should avoid including subtrees that only contribute negative values to the sum.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(h)
Topics
This problem involves: Dynamic Programming, Tree, Depth-First Search, Binary Tree.
Companies
Asked at: Arcesium, Arista Networks, Baidu, Citadel, Datadog, Directi, DoorDash, Flipkart, Hotstar, Intuit, Nutanix, Nvidia, Oracle, Patreon, Salesforce, Samsung, Snap, TikTok, Uber, Walmart Labs, Wix, Yandex.