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.
Specifically, how does the number of steps change when the number of nodes increases?
Analyze the time complexity of the following code snippet.
int maxPathSumHelper(struct TreeNode* root, int* maxSum) {
if (root == NULL) return 0;
int left = maxPathSumHelper(root->left, maxSum);
int right = maxPathSumHelper(root->right, maxSum);
int maxSingle = fmax(fmax(left, right) + root->val, root->val);
int maxTop = fmax(maxSingle, left + right + root->val);
*maxSum = fmax(*maxSum, maxTop);
return maxSingle;
}
int maxPathSum(struct TreeNode* root) {
int maxSum = INT_MIN;
maxPathSumHelper(root, &maxSum);
return maxSum;
}
This code finds the maximum path sum in a binary tree by visiting each node once and calculating the best path through it.
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 (n) grows, the function visits each node once, doing a fixed amount of work per node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of operations grows directly with the number of nodes.
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 some nodes multiple times, so time is more than linear."
[OK] Correct: Each node is visited once in the recursion, and results are combined on the way back, so no repeated work happens.
Understanding this linear time complexity helps you explain how efficient your solution is and shows you can analyze recursive tree problems clearly.
"What if the tree was very unbalanced and looked like a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the path definition
The path can start and end at any two nodes, not necessarily root or leaf.Step 2: Identify maximum sum meaning
It is the largest sum of node values along any such path in the tree.Final Answer:
The largest sum of values along any path between two nodes -> Option CQuick Check:
Maximum path sum means largest sum on any path [OK]
- Thinking it must start at root
- Confusing with sum of all nodes
- Considering only leaf nodes
struct TreeNode?Solution
Step 1: Understand parameter passing for trees in C
Binary trees are usually passed as pointers to nodes, sostruct TreeNode*is correct.Step 2: Check return type and parameter correctness
Function returns int (maximum sum), and parameter is pointer to root node, soint maxPathSum(struct TreeNode* root);is correct.Final Answer:
int maxPathSum(struct TreeNode* root); -> Option AQuick Check:
Use pointer parameter and int return type [OK]
- Passing struct by value instead of pointer
- Using void return type instead of int
- Using double pointer unnecessarily
1
/ \
2 3
Solution
Step 1: Identify all possible paths and their sums
Paths: 2->1->3 sum = 6, 1->2 sum = 3, 1->3 sum = 4, single nodes 2,3,1.Step 2: Find the maximum sum among these paths
Maximum sum is 6 from path 2->1->3.Final Answer:
6 -> Option BQuick Check:
Sum of 2 + 1 + 3 = 6 [OK]
- Considering only root to leaf paths
- Ignoring paths that go through root connecting children
- Choosing max node value instead of path sum
int maxPathSumUtil(struct TreeNode* root, int* maxSum) {
if (!root) return 0;
int left = maxPathSumUtil(root->left, maxSum);
int right = maxPathSumUtil(root->right, maxSum);
int maxSingle = max(max(left, right) + root->val, root->val);
int maxTop = max(maxSingle, left + right + root->val);
*maxSum = max(*maxSum, maxTop);
return maxSingle;
}
What is the main bug in this code?Solution
Step 1: Check usage of max function
The code uses max() but C standard library does not have max function by default; it must be defined or replaced.Step 2: Verify other logic parts
Return and pointer update logic is correct; negative values handled by max comparisons.Final Answer:
It uses max function without defining it -> Option DQuick Check:
max() must be defined or replaced in C [OK]
- Assuming max() is built-in in C
- Confusing return values with maxSum update
- Ignoring pointer usage correctness
Solution
Step 1: Understand handling of negative values in max path sum
Negative values can reduce path sums, so recursion must consider them but choose max sums carefully.Step 2: Use recursion with max comparisons to update global max
Recursion returns max single path sum including negative values, updating global max with max of left+right+node or single path sums.Final Answer:
Use recursion to compute max path sums including negative values and update global max accordingly -> Option AQuick Check:
Include negatives but choose max sums carefully [OK]
- Ignoring negative nodes completely
- Treating negatives as zero forcibly
- Using simple sums without max comparisons
Ready to prove it?
Pass the test and this pattern moves unknown
