</>

124. Binary Tree Maximum Path Sum

Hard NeetCode 150
Trees Time O(N) Space O(H)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Trees
Time
O(N)
Space
O(H)

Standard Solution (C++)

SummaryGiven a binary tree where each node holds an integer value, find the maximum sum achievable by any path — where a path is any sequence of adjacent nodes with no node visited twice and no direction constraint.

Approach
  • Use a post-order DFS that, at each node, computes the best "arm" (one-sided path that can extend upward to the parent) and updates a global answer.
  • The arm value is max(0, left_arm, right_arm) + node->val; clamping with 0 discards subtrees that would drag the sum negative.
  • The path that peaks at this node connects both arms: left_arm + node->val + right_arm; compare this against the running global max.
  • Return only the single-arm value upward so the caller can legally extend the path in one direction.
#include <algorithm>
#include <climits>
using namespace std;

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right)
        : val(x), left(left), right(right) {}
};

class Solution {
    int ans = INT_MIN;

    // Returns the best single-arm gain this subtree can contribute upward.
    int dfs(TreeNode* node) {
        if (!node) return 0;

        // Clamp negative arms to 0 — never worth including them.
        int left  = max(0, dfs(node->left));
        int right = max(0, dfs(node->right));

        // Path that bends here (cannot extend further upward).
        ans = max(ans, left + node->val + right);

        // Best arm we can pass to the parent (only one side).
        return node->val + max(left, right);
    }

public:
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return ans;
    }
};
Time O(n) Space O(h), where h is the height of the tree (recursion stack)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
    std::pair<int, int> dfs(TreeNode* root){
        if(!root){
            return {-1001, -1001};
        }
        auto [l1, l2]=dfs(root->left);
        auto [r1, r2]=dfs(root->right);
        int v=root->val;
        int ableToConnect=std::max(std::max(l1, r1)+v, v);
        int notAbleToConnet=std::max(std::max(l2, r2), std::max(l1+r1+v, ableToConnect));
        return {ableToConnect, notAbleToConnet};
    }
    int maxPathSum(TreeNode* root) {
        auto [v1, v2]=dfs(root);
        return std::max(v1, v2);
    }
};

// Time Complexity: O(N)
// Space Complexity: O(H), where H is the height of the tree due to recursion stack.
Time O(N) Space O(H)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern