FastPrepBinary Tree Maximum Path Sum
Problems/Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum

Globalization Partners logoGlobalization Partners● HardFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report directly links the public Binary Tree Maximum Path Sum task and asks for pseudocode. The judged core task matches the visible source at about 100%.

A path in a binary tree is a non-empty sequence of nodes in which consecutive nodes share an edge and no node appears more than once. Its sum is the total of its node values.

Given the root of a non-empty binary tree, return the maximum path sum. The path may start and end at any nodes and does not need to pass through the root.

Function

maxPathSum(root: TreeNode) → int

Examples

Example 1

root = [1,2,3]return = 6

The path 2 -> 1 -> 3 uses both children and has sum 6.

Example 2

root = [-10,9,20,null,null,15,7]return = 42

The maximum path is 15 -> 20 -> 7, whose sum is 42.

Example 3

root = [-3]return = -3

A path must contain at least one node, so the only valid answer is the root value -3.

Constraints

  • The tree contains between 1 and 3 * 10^4 nodes.
  • -1000 <= Node.val <= 1000

More Globalization Partners problems

  • Maximum Number of Fish in a GridONSITE INTERVIEW · Seen Jun 2026
  • Recover Binary Search TreeONSITE INTERVIEW · Seen Jun 2026
See Globalization Partners hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxPathSum(TreeNode root) {
        // write your code here
    }
}
Checking account…