
Problem
Solution
Submissions
Maximum Path Sum in Binary Tree
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 8
Write a C# function to find the maximum path sum in a binary tree. A path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and not need to go through the root. The path sum is the sum of the values of the nodes in the path.
Example 1
- Input: root = [1, 2, 3]
- Output: 6
- Explanation:
- The maximum path sum is the path [2, 1, 3], with sum = 6.
Example 2
- Input: root = [-10, 9, 20, null, null, 15, 7]
- Output: 42
- Explanation:
- The maximum path sum is the path [15, 20, 7], with sum = 42.
Constraints
- The number of nodes in the tree is in the range [1, 3 * 10^4]
- -1000 ≤ Node.val ≤ 1000
- Time Complexity: O(n)
- Space Complexity: O(h), where h is the height of the tree
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use recursion to traverse the tree
- For each node, compute the maximum path sum that includes the node as the highest point
- Update the global maximum path sum during traversal
- Handle negative values appropriately
- Consider both including and excluding the current node in the path