πŸš€Day 8. Maximum path sum from any node 🧠

The problem can be found at the following link: Problem Link

πŸ’‘ Problem Description:

Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree.

πŸ” Example Walkthrough:

Input: root[] = [10, 2, 10, 20, 1, N, -25, N, N, N, N, 3, 4] Output: 42 Explanation:

The maximum path sum is represented using the green colored nodes in the binary tree.

Input: root[] = [-17, 11, 4, 20, -2, 10] Output: 31 Explanation:

The maximum path sum is represented using the green colored nodes in the binary tree.

Constraints:

  • 1 ≀ number of nodes ≀ $10^3$

  • -10^4 ≀ node->data ≀ $10^4$

🎯 My Approach:

  1. DFS Post-Order Traversal: Use a recursive DFS (post-order) approach to calculate, for each node:

    • maxSingle: The maximum path sum including the node and at most one of its subtrees.

    • Global Maximum: Update a global result with the sum of the node value and the maximum contributions from both left and right subtrees.

  2. Steps:

    • Recursively compute the maximum path sum from the left and right children.

    • Discard negative sums by taking max(0, value).

    • Update the global maximum with left + right + node->data.

    • Return node->data + max(left, right) to allow parent nodes to include the maximum available contribution.

πŸ•’ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as each node is visited exactly once.

  • Expected Auxiliary Space Complexity: O(h), where h is the height of the tree (due to the recursion stack). In the worst-case (skewed tree), this can be O(n).

πŸ“ Solution Code

Code (C++)

🌲 Alternative Approaches

2️⃣ Bottom-Up Dynamic Programming Approach

Algorithm

  • Use post-order traversal to compute two values for each node:

    • maxSingle: Maximum path sum including the current node and at most one child.

    • maxTop: Maximum path sum where the current node is the highest node (root of the path).

  • Recursively compute these values and return the global maximum.

3️⃣ Iterative Post-Order Traversal Using Stack

Algorithm

  • Perform an iterative post-order traversal using a stack.

  • Maintain a map to store the maximum path sum for each node.

  • For each node, compute:

    • maxSingle: Max sum including the node and one child.

    • maxTop: Max sum through the node with both children.

πŸ” Comparison of Approaches

Approach

Time Complexity

Space Complexity

Pros

Cons

Recursive DFS (Optimized)

🟒 O(N)

🟑 O(H) (stack space)

Clean, concise, easy to implement

Stack overflow for deep trees

Bottom-Up DP

🟒 O(N)

🟑 O(H)

Explicit DP states, easy to debug

Slightly verbose

Iterative Post-Order (Stack)

🟒 O(N)

🟑 O(H)

Avoids recursion stack overflow

More complex logic, verbose

πŸš€ Best Choice?

  • βœ… Balanced Trees: Recursive DFS is simple and fast.

  • βœ… Very Deep Trees: Iterative post-order avoids stack overflow.

  • βœ… For Debugging/DP: Bottom-Up DP gives clear intermediate states.

Code (Java)

Code (Python)

🎯 Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated