Questions with My Top-Down Definition Pattern

Path Sum

Estimated reading: 1 minute 62 views
				
					public boolean hasPathSum(TreeNode root, int targetSum) {
    if (root == null) {
        return false;
    }

    if (root.left == null && root.right == null) {
        return targetSum == root.val;
    }

    int newTargetSum = targetSum - root.val;
    return hasPathSum(root.left, newTargetSum) || hasPathSum(root.right, newTargetSum);
}

				
			
Share this Doc

Path Sum

Or copy link

CONTENTS