Problem
You get the root root of a binary tree. In a binary tree, each node has at most two children: a left child and a right child.
A path is a sequence of nodes in which an edge connects each node to the next node. An edge is the link between a node and its child. A path contains each node at most one time.
A path has at least one node. It does not have to contain the root. The path sum is the sum of the node values on the path.
Return the largest path sum of all the paths in the tree.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- root = [2,-1,3]
- Output:
- 5
- Explanation:
- The path
2 -> 3has the sum 5. The path-1 -> 2 -> 3has the smaller sum 4.
Example 2
- Input:
- root = [-3,10,-20,6,7]
- Output:
- 23
- Explanation:
- The best path is
6 -> 10 -> 7, with the sum 23. It does not contain the root.
Example 3
- Input:
- root = [-7]
- Output:
- -7
- Explanation:
- A path must have at least one node. The only path is the root.
Constraints
- 1 <= number of nodes <= 3 * 10^4
- -1000 <= Node.val <= 1000
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Binary Tree Cameras
Coding · Binary Tree · Depth-First Search · DP on Trees
- Diameter of Binary Tree
Coding · Binary Tree · Depth-First Search · DP on Trees
- Binary Search Tree to Greater Sum Tree
Coding · Binary Search Tree · Binary Tree · Depth-First Search
- Maximum Depth of Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 7 records
Amazon
Leetcode 124. Binary Tree Maximum Path Sum
Reported May 19, 2026 · 2 source reports
JUNIOR, MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceDatadog
Leetcode 124. Binary Tree Maximum Path Sum
Reported Mar 15, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceDoorDash
Leetcode 124. Binary Tree Maximum Path Sum
Reported Feb 22, 2026 · 7 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceExpedia
Leetcode 124. Binary Tree Maximum Path Sum
Reported Jun 24, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026