public static boolean hasPathSum(TreeNode root, int sum) {

    if (root == null) {
      return false;
    }

    int leftSum = sum - root.val;

    // find the leave node whose path adds to sum
    if (leftSum == 0 && root.left == null && root.right == null) {
      return true;
    }

    return hasPathSum(root.left, leftSum) || hasPathSum(root.right, leftSum);
  }