Maximum Path Sum in Binary Tree in DSA C++ - Time & Space Complexity
We want to understand how the time needed to find the maximum path sum in a binary tree grows as the tree gets bigger.
The question is: how does the number of steps change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
int maxPathSumHelper(TreeNode* node, int& maxSum) {
if (!node) return 0;
int left = max(0, maxPathSumHelper(node->left, maxSum));
int right = max(0, maxPathSumHelper(node->right, maxSum));
maxSum = max(maxSum, left + right + node->val);
return max(left, right) + node->val;
}
int maxPathSum(TreeNode* root) {
int maxSum = INT_MIN;
maxPathSumHelper(root, maxSum);
return maxSum;
}
This code finds the maximum sum of values along any path in a binary tree, where a path can start and end at any nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Each node is visited exactly one time.
As the number of nodes in the tree increases, the function visits each node once, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The steps increase in a straight line as the tree grows.
Time Complexity: O(n)
This means the time to find the maximum path sum grows linearly with the number of nodes in the tree.
[X] Wrong: "The recursion visits nodes multiple times, so the time is more than linear."
[OK] Correct: Each node is visited once, and results are combined on the way back, so no repeated work happens.
Understanding this linear time complexity helps you explain efficient tree traversal and recursion skills clearly in interviews.
"What if the tree was very unbalanced and looked like a linked list? How would the time complexity change?"
Practice
Maximum Path Sum in a binary tree mean?Solution
Step 1: Understand the path definition
A path in a binary tree can start and end at any nodes, not necessarily root or leaf.Step 2: Define maximum path sum
It is the highest sum of node values along any such path in the tree.Final Answer:
The largest sum of values along any path between two nodes in the tree -> Option BQuick Check:
Maximum path sum means highest sum on any path [OK]
- Confusing maximum path sum with sum of all nodes
- Thinking path must start at root
- Considering only leaf nodes for path ends
Solution
Step 1: Understand parameter passing
The function needs to update maxSum globally, so it must be passed by reference.Step 2: Check pointer usage
TreeNode* is used to pass node pointers, not objects directly.Final Answer:
int maxPathSumHelper(TreeNode* root, int& maxSum); -> Option AQuick Check:
Use pointer and reference for maxSum [OK]
- Passing TreeNode by value instead of pointer
- Not passing maxSum by reference
- Using void return type instead of int
1
/ \
2 3
What is the maximum path sum returned by the standard algorithm?Solution
Step 1: Calculate path sums including root
Paths: 2->1->3 sum = 6, 2->1 sum = 3, 1->3 sum = 4.Step 2: Identify maximum sum
The maximum path sum is 6 from path 2->1->3.Final Answer:
6 -> Option DQuick Check:
2+1+3 = 6 [OK]
- Considering only root-to-leaf paths
- Ignoring paths passing through root
- Adding only one child value
int maxPathSumHelper(TreeNode* root, int& maxSum) {
if (!root) return 0;
int left = maxPathSumHelper(root->left, maxSum);
int right = maxPathSumHelper(root->right, maxSum);
int current = root->val + left + right;
maxSum = max(maxSum, current);
return root->val + max(left, right);
}
What is the bug?Solution
Step 1: Analyze negative path sums handling
The code adds left and right directly without ignoring negative sums, which can reduce maxSum.Step 2: Correct approach for negatives
Should use max(0, left) and max(0, right) to ignore negative sums.Final Answer:
It does not ignore negative path sums -> Option AQuick Check:
Negative sums must be ignored to maximize path sum [OK]
- Adding negative child sums reduces maxSum
- Returning sum of both children instead of max child
- Not checking for null root
-10
/ \
9 20
/ \
15 7
Choose the correct maximum path sum.Solution
Step 1: Calculate max path sums for subtrees
Left subtree max path sum is 9. Right subtree max path sum includes 20 + 15 + 7 = 42.Step 2: Consider paths through root
Path through root: -10 + max(0,9) + max(0,15+20+7) = -10 + 9 + 42 = 41, less than 42.Step 3: Identify maximum path sum
The maximum path sum is 42 from path 15->20->7.Final Answer:
42 -> Option CQuick Check:
15+20+7 = 42 [OK]
- Including negative root value in max path sum
- Ignoring paths not passing through root
- Adding left and right children without max(0, val)
Ready to prove it?
Pass the test and this pattern moves unknown
