
									 Problem
								
								
									 Solution
								
								
									 Submissions
								
								
							Binary Tree Maximum Path Sum
								Certification: Advanced Level
								Accuracy: 0%
								Submissions: 0
								Points: 15
							
							Write a C program to find the maximum path sum in a binary tree. A path is defined as any route along parent-child connections from some starting node to any ending node. The path must contain at least one node and does not need to go through the root.
Example 1
- Input: root = [1,2,3]
- Output: 6
- Explanation: - Tree structure: 1 is root, 2 and 3 are its children. 
- Possible paths: [2], [1], [3], [2,1], [1,3], [2,1,3]. 
- Path [2,1,3] gives maximum sum: 2 + 1 + 3 = 6. 
- Therefore, maximum path sum is 6.
 
- Tree structure: 1 is root, 2 and 3 are its children. 
Example 2
- Input: root = [-10,9,20,null,null,15,7]
- Output: 42
- Explanation: - Tree has -10 as root, 9 and 20 as children of root, 15 and 7 as children of 20. 
- The optimal path is [15,20,7] which doesn't include the root. 
- Path sum: 15 + 20 + 7 = 42. 
- Therefore, maximum path sum is 42.
 
- Tree has -10 as root, 9 and 20 as children of root, 15 and 7 as children of 20. 
Constraints
- The number of nodes in the tree is in the range [1, 3 * 10^4]
- -1000 ≤ Node.val ≤ 1000
- Time Complexity: O(n) where n is number of nodes
- Space Complexity: O(h) where h is height of tree (recursion stack)
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 post-order traversal to process children before parent nodes
- For each node, calculate maximum path sum that ends at that node
- Consider three cases: path through left child, path through right child, or path through both children
- Keep track of global maximum path sum encountered during traversal
- Return the maximum single-branch path sum to parent for further calculations
- Handle negative path sums by choosing 0 instead (don't include negative branches)
