Binary Tree Maximum Path Sum
Updated
Problem statement
Given the root of a non-empty binary tree whose nodes contain integers, return the maximum sum of a non-empty path.
A path is a sequence of distinct nodes where consecutive nodes share an edge. The path may start and end at any nodes and does not need to pass through the root. A node may appear at most once in the path.
Function
maxPathSum(root: TreeNode) → intExamples
Example 1
root = [1,2,3]return = 6The path 2 -> 1 -> 3 has sum 6.
Example 2
root = [-10,9,20,null,null,15,7]return = 42The best path is 15 -> 20 -> 7, whose sum is 42.
Example 3
root = [-3]return = -3A path must contain at least one node, so the single node is the answer.
Constraints
- The tree contains between
1and5000nodes. - Each node value is between
-1000and1000. - The tree height is at most
1000.
More Goldman Sachs Onsite Interview questions
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Dungeon HealthONSITE INTERVIEW · Seen Sep 2026
- Insert, Delete, And Get Random In Constant TimeONSITE INTERVIEW · Seen Sep 2026
- Least Recently Used CacheONSITE INTERVIEW · Seen Sep 2026
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Sum of Weighted Distances in a TreeGoogle · ONSITE INTERVIEWGoogle interview questions
- File Encryption Tree OptimizationDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Binary Tree Maximum Path SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Binary Tree Maximum Path SumGlobalization Partners · ONSITE INTERVIEW
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Transitive Employee Referral CountsRobinhood · ONSITE INTERVIEWRobinhood interview questions
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEWAdobe interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Maximum Nonadjacent Sum in a General TreeZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions