Path Sum in Binary Tree with DFS Approach

🚀 #100DaysOfCode | Day 36 📌 LeetCode – Path Sum Today I solved the Path Sum problem using a recursive Depth-First Search (DFS) approach. Given a binary tree and a target sum, determine whether there exists a root-to-leaf path such that the sum of all node values equals the target. 💡 Approach: ✔ If the node is null, return false. ✔ If it's a leaf node, check whether the remaining sum equals the node value. ✔ Recursively subtract the current node value from targetSum. ✔ Check both left and right subtrees. ✔ If either returns true → path exists. ⏱ Complexity: Time Complexity: O(n) Space Complexity: O(h) — height of the tree Binary tree problems are all about mastering recursion and understanding base cases clearly. #Java #LeetCode #DSA #BinaryTree #Recursion #ProblemSolving

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories