import java.util.Objects;class Solution { private boolean solve(TreeNode root, int sum) { if (Objects.isNull(root)) { return false; } if (Objects.isNull(root.left) && Objects.isNull(root.right)) { return root.val == sum; } return solve(root.left, sum - root.val) || solve(root.right, sum - root.val); } public boolean hasPathSum(TreeNode root, int sum) { return solve(root, sum); }}