Binary Tree Maximum Path Sum on LeetCode with DFS and Recursion

Day 83 of #100DaysOfCode Today I solved "Binary Tree Maximum Path Sum" on LeetCode using DFS + Recursion. Key Idea: At every node, we calculate the maximum path sum passing through it. But here’s the twist We ignore negative paths because they reduce the total sum. Approach: • Recursively get the max path sum from left and right subtrees • Ignore negative values using max(0, …) • Update the global answer as: root->val + left + right • Return to parent: root->val + max(left, right) This ensures we consider both: Path passing through the node Path extending upwards Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Greedy choice (ignore negative paths) Time Complexity: O(n) Space Complexity: O(h) This problem really sharpened my understanding of handling multiple path possibilities in trees From simple traversals to advanced patterns — growth is visible #Day83 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories