Maximum Path Sum in Binary Tree in DSA Typescript - 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.
function maxPathSum(root: TreeNode | null): number {
let maxSum = -Infinity;
function helper(node: TreeNode | null): number {
if (!node) return 0;
const left = Math.max(helper(node.left), 0);
const right = Math.max(helper(node.right), 0);
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, right);
}
helper(root);
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 node.
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 during the recursion.
As the number of nodes grows, the function visits each node once, so the total steps grow roughly the same as the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of operations grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the maximum path sum grows directly in proportion to 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 full visits happen.
Understanding this time complexity helps you explain how your solution scales and shows you can analyze recursive tree problems clearly.
"What if the tree was balanced vs very skewed? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the maximum sum of values along any path in the binary tree, where a path can start and end at any nodes.Step 2: Differentiate from other tree sums
This is different from longest path or total sum; it focuses on the maximum sum achievable on any path.Final Answer:
The highest sum of node values along any path in the tree -> Option AQuick Check:
Maximum path sum = highest node value sum [OK]
- Confusing path sum with longest path length
- Summing all nodes instead of a path
- Looking for minimum instead of maximum
Solution
Step 1: Identify correct parameter type
The function should accept a binary tree node or null, so TreeNode | null is correct.Step 2: Identify correct return type
The function returns a number representing the maximum path sum, so return type is number.Final Answer:
function maxPathSum(root: TreeNode | null): number -> Option BQuick Check:
Input: TreeNode or null, Output: number [OK]
- Using array or string instead of TreeNode
- Returning void instead of number
- Not allowing null as input
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function maxPathSum(root: TreeNode | null): number {
let maxSum = -Infinity;
function helper(node: TreeNode | null): number {
if (!node) return 0;
const left = Math.max(helper(node.left), 0);
const right = Math.max(helper(node.right), 0);
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, right);
}
helper(root);
return maxSum;
}
const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(maxPathSum(tree));Solution
Step 1: Calculate max path sums for each node
Node 2 and 3 have no children, so max path sums are 2 and 3. For root 1, left=2, right=3, total=1+2+3=6.Step 2: Determine maximum path sum
Maximum path sum is max of all nodes' sums: max(2,3,6) = 6.Final Answer:
6 -> Option DQuick Check:
Sum(1+2+3) = 6 [OK]
- Ignoring zero floor for negative sums
- Returning sum of only root and one child
- Confusing return value with maxSum variable
function maxPathSum(root: TreeNode | null): number {
let maxSum = 0;
function helper(node: TreeNode | null): number {
if (!node) return 0;
const left = helper(node.left);
const right = helper(node.right);
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, right);
}
helper(root);
return maxSum;
}Solution
Step 1: Check maxSum initialization
maxSum initialized to 0 can cause wrong result if all node values are negative.Step 2: Correct initialization for negative values
Initialize maxSum to -Infinity to handle negative node values correctly.Final Answer:
maxSum initialized to 0 instead of -Infinity -> Option CQuick Check:
maxSum must start at -Infinity for negative nodes [OK]
- Starting maxSum at 0 causes wrong max for negative trees
- Forgetting base case returns 0
- Using min instead of max for return
Solution
Step 1: Calculate max path sums for subtrees
Right subtree max path: 15 + 20 + 7 = 42. Left subtree max is 9. Root is -10.Step 2: Determine overall max path sum
Max path sum is max of 42 (right subtree path), 9 (left), and paths including root. The max is 42.Final Answer:
42 -> Option AQuick Check:
Sum(15+20+7) = 42 [OK]
- Ignoring path through both children and root
- Choosing sum of root and one child only
- Not considering negative root value impact
Ready to prove it?
Pass the test and this pattern moves unknown
