It is the largest sum of values from any path in the tree. The path can start and end at any nodes, but must follow parent-child connections.
Click to reveal answer
beginner
Why do we use a recursive approach to find the maximum path sum in a binary tree?
Because each node's maximum path sum depends on the maximum sums of its left and right children, recursion helps break down the problem into smaller parts.
Click to reveal answer
intermediate
In the maximum path sum problem, what does the helper function usually return?
It returns the maximum sum of a path starting from the current node and extending down to one child or none, to be used by its parent.
Click to reveal answer
intermediate
How do we update the global maximum path sum during recursion?
At each node, we calculate the sum of the node's value plus the maximum sums from left and right children (if positive). If this sum is greater than the current global max, we update it.
Click to reveal answer
beginner
What is the time complexity of the maximum path sum algorithm in a binary tree?
It is O(n), where n is the number of nodes, because we visit each node once during the recursion.
Click to reveal answer
What does the maximum path sum in a binary tree represent?
AThe sum of values from root to leaf only
BThe sum of all node values in the tree
CThe largest sum of values along any path between two nodes
DThe maximum value of a single node
✗ Incorrect
The maximum path sum is the largest sum along any path connecting two nodes, not necessarily from root to leaf.
In the recursive solution, what do we do if the left or right child's maximum path sum is negative?
AIgnore it by treating it as zero
BSubtract it from the current node's value
CInclude it anyway
DStop recursion
✗ Incorrect
Negative sums reduce the total, so we ignore them by treating negative sums as zero.
What is the role of the global variable in the maximum path sum algorithm?
ATo count the number of nodes
BTo store the maximum path sum found so far
CTo store the current node's value
DTo keep track of recursion depth
✗ Incorrect
The global variable keeps track of the highest path sum found during the traversal.
Which traversal method is naturally used in the maximum path sum recursive solution?
ALevel-order
BIn-order
CPre-order
DPost-order
✗ Incorrect
Post-order traversal is used because we need results from left and right children before processing the current node.
What is the space complexity of the maximum path sum algorithm in a balanced binary tree?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
The space complexity is O(log n) due to the recursion stack in a balanced tree.
Explain how recursion helps find the maximum path sum in a binary tree.
Think about how each node depends on its children.
You got /4 concepts.
Describe the steps to update the global maximum path sum during traversal.
Focus on what happens at each node.
You got /4 concepts.
Practice
(1/5)
1. What does the "Maximum Path Sum in Binary Tree" problem ask you to find?
easy
A. The sum of all node values in the tree
B. The longest path length in the binary tree
C. The highest sum of values along any path between two nodes in the tree
D. The maximum value of a single node in the tree
Solution
Step 1: Understand the problem definition
The problem asks for the maximum sum of node values along any path in the binary tree. A path can start and end at any nodes, but must be connected.
Step 2: Differentiate from other path problems
This is not about longest path length or total sum of all nodes, but the maximum sum of values on any path.
Final Answer:
The highest sum of values along any path between two nodes in the tree -> Option C
Quick Check:
Maximum path sum = highest sum path [OK]
Hint: Focus on sum of connected nodes, not length or single node [OK]
Common Mistakes:
Confusing maximum path sum with longest path length
Thinking path must start at root
Summing all nodes instead of a path
2. Which of the following is the correct base case return value for a recursive function computing maximum path sum in a binary tree node when the node is null?
easy
A. -Infinity
B. 1
C. null
D. 0
Solution
Step 1: Consider the meaning of base case for null node
When the node is null, the maximum gain from this subtree is 0 (choose not to extend path here).
Step 2: Why not others?
-Infinity works only with Math.max(0, result) but 0 is standard and cleaner. Returning 0 allows proper clamping in parent. null or 1 cause type/math errors.
Final Answer:
0 -> Option D
Quick Check:
Base case for null = 0 [OK]
Hint: Return 0 for null to represent zero contribution [OK]
Common Mistakes:
Returning -Infinity without Math.max(0, ·)
Returning null causing type errors
Returning positive values skewing max
3. What is the output of the following code snippet for maximum path sum?
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function maxPathSum(root) {
let maxSum = -Infinity;
function dfs(node) {
if (!node) return 0;
let left = Math.max(dfs(node.left), 0);
let right = Math.max(dfs(node.right), 0);
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, right);
}
dfs(root);
return maxSum;
}
const root = new TreeNode(-10, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));
console.log(maxPathSum(root));
medium
A. 42
B. 35
C. 27
D. 20
Solution
Step 1: Trace dfs on left subtree
Left child 9: left=0, right=0, maxSum = 9, returns 9.
Root: left=9, right=35, path sum -10+9+35=34 <42, maxSum remains 42.
Final Answer:
42 -> Option A
Quick Check:
Max path sum = 42 [OK]
Hint: Add node + max(left, right) and update maxSum with both sides [OK]
Common Mistakes:
Not ignoring negative sums by using Math.max with 0
Adding left and right children incorrectly
Confusing return value with maxSum update
4. Identify the bug in this maximum path sum function and select the fix:
function maxPathSum(root) {
let maxSum = -Infinity;
function dfs(node) {
if (!node) return -Infinity;
let left = dfs(node.left);
let right = dfs(node.right);
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, right);
}
dfs(root);
return maxSum;
}
medium
A. Add check to ignore negative sums with Math.max(left, 0)
B. Change base case return from -Infinity to 0
C. Initialize maxSum to 0 instead of -Infinity
D. Return maxSum inside dfs instead of outside
Solution
Step 1: Analyze base case and recursive calls
Returning -Infinity for null nodes causes left and right to be -Infinity, which breaks addition and max calculations.
Step 2: Fix by ignoring negative sums
Use Math.max(dfs(node.left), 0) and Math.max(dfs(node.right), 0) to ignore negative paths and avoid incorrect sums.
Final Answer:
Add check to ignore negative sums with Math.max(left, 0) -> Option A
Quick Check:
Ignore negative sums with Math.max(..., 0) [OK]
Hint: Ignore negative child sums by max with 0 to fix bugs [OK]
Common Mistakes:
Returning -Infinity breaks addition
Not ignoring negative sums from children
Changing maxSum initialization wrongly
5. Given a binary tree where some nodes have negative values, how would you modify the maximum path sum algorithm to handle the case where all values are negative?
hard
A. Allow paths to be empty and return 0 if all sums are negative
B. Track the maximum single node value separately and compare with path sums
C. Use Math.max with 0 on child sums to ignore negatives
D. Initialize maxSum to -Infinity and do not ignore negative child sums
Solution
Step 1: Understand all-negative case
The max path sum is the highest (least negative) single node value. Paths must include at least one node.
Step 2: Key modification
Tracking the maximum single node value separately ensures that if all child sums are negative, the algorithm still returns the maximum node value. Using Math.max(0, dfs(child)) ignores negative child sums but can miss the max single node if all are negative.
Final Answer:
Track the maximum single node value separately and compare with path sums -> Option B
Quick Check:
Separate max node tracking handles all-negative correctly [OK]
Hint: Track max single node separately to handle all-negative cases [OK]
Common Mistakes:
Allowing empty path return 0 (invalid)
Not tracking max node separately (misses max negative node)
Not ignoring negative child sums (misses single nodes)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.