Skip to content
Binary TreesEasy

27. Maximum Path Sum ⭐

Finds maximum path sum where path can start and end at any node (LeetCode Hard).

Time O(n) Space O(h)
Open in 3D Studio

Live loop — open the studio to scrub, add your own array, or race it in Battle Arena.

Asked in interviews at

MicrosoftAppleBloomberg

Reference implementation (Python)

def max_path_sum(root):
    max_sum = float('-inf')
    def gain(node):
        nonlocal max_sum
        if not node: return 0
        left_gain = max(gain(node.left), 0)
        right_gain = max(gain(node.right), 0)
        price_newpath = node.val + left_gain + right_gain
        max_sum = max(max_sum, price_newpath)
        return node.val + max(left_gain, right_gain)
    gain(root)
    return max_sum

Embed this visualization

Drop a live, looping 27. Maximum Path Sum ⭐ canvas into a blog post or course page.

<iframe src="/embed/tree-max-path-sum" width="100%" height="380" style="border:1px solid #e6e8ec;border-radius:14px" loading="lazy" title="3D algorithm visualization by CodeWithMunnaX"></iframe>

Frequently asked

What is the time and space complexity of 27. Maximum Path Sum ⭐?

27. Maximum Path Sum ⭐ runs in O(n) time and O(h) auxiliary space.

Is the 27. Maximum Path Sum ⭐ visualizer free?

The core 3D visualizer, Web Audio chimes and step intuition are free. A one-time ₹49 Pro Pass unlocks advanced algorithms, large custom arrays, Battle Arena and reel export.

Which companies ask 27. Maximum Path Sum ⭐ in interviews?

27. Maximum Path Sum ⭐ shows up in interviews at Microsoft, Apple, Bloomberg.

How does the 3D visualization of 27. Maximum Path Sum ⭐ work?

Each array value becomes a 3D pillar whose height maps to its value. Comparisons glow amber, swaps flash red, and settled elements lock green while a floating pointer badge tracks the active index — synchronized line-by-line with the Python code.

More Binary Trees