Tensorum
YouTubeশেখা শুরু করুন
LeetCode 124 · Tree DP

Binary Tree Maximum Path Sum

LeetCode 124 · কঠিন · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Update the answer with both sides; return only one side to the parent.

কঠিনDynamic ProgrammingTreeDepth-First SearchBinary TreeDP on Trees
LeetCode-এ খুলুন ↗

আমি Binary Tree Maximum Path Sum (LeetCode 124) এভাবে সমাধান করেছি — Tree DP প্যাটার্নের একটি কঠিন প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Every path has a highest node where it bends. At each node, the best path bending there is node + best downward gain from the left + best downward gain from the right. But a path passed up to the parent can only continue down one side, so return node + the better single side. Negative gains are dropped as 0.

Approach

  1. 1

    dfs(node) returns the best downward path sum starting at node.

  2. 2

    Left and right gains are clipped at 0 — a negative branch is better left out.

  3. 3

    Update the global best with node.val + left + right (the bend at this node).

  4. 4

    Return node.val + max(left, right) to the parent.

আমি কীভাবে সমাধান করেছি

Post-order DFS (tree DP)PythonO(n) time · O(h) space
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        max_sum = float("-inf")
        def dfs(node):
            nonlocal max_sum
            if node==None:
                return 0
            left = max(0,dfs(node.left))
            right = max(0,dfs(node.right))
            max_sum=max(max_sum,node.val+left+right)
            return node.val+max(left,right)
        dfs(root)
        return max_sum
স্টেপ-থ্রু লোড হচ্ছে…

Binary Tree Maximum Path Sum — সাধারণ প্রশ্ন

Binary Tree Maximum Path Sum-এর পেছনে intuition কী?

Every path has a highest node where it bends. At each node, the best path bending there is node + best downward gain from the left + best downward gain from the right. But a path passed up to the parent can only continue down one side, so return node + the better single side. Negative gains are dropped as 0.

Binary Tree Maximum Path Sum (LeetCode 124) কীভাবে সমাধান করবেন?

1. dfs(node) returns the best downward path sum starting at node. 2. Left and right gains are clipped at 0 — a negative branch is better left out. 3. Update the global best with node.val + left + right (the bend at this node). 4. Return node.val + max(left, right) to the parent.

Binary Tree Maximum Path Sum-এর time আর space complexity কত?

Time: O(n)। Space: O(h) recursion। (Accepted approach: Post-order DFS (tree DP)।)

Binary Tree Maximum Path Sum-এ সাধারণ ভুলগুলো কী?

Start the global best at −∞ — an all-negative tree must return its largest value. Returning both sides to the parent would create a forked, invalid path.