Path Sum (Leetcode 112)

Technique: FAITH technique of Recursion

Problem Link: https://leetcode.com/problems/path-sum/

class Solution:

    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        
        def fun(root, targetSum):
            
            if(root is None):
                return False
            if(root.left is None and root.right is None and root.val == targetSum):
                return True
            lans=fun(root.left, targetSum-root.val)
            rans=fun(root.right, targetSum-root.val)
            if(lans or rans):
                return True
            return False
            
        ans=fun(root, targetSum)
        return ans

Last updated