MADRID - It took the White House more than seven years to lock in Spain on President Barack Obama's foreign travel schedule. But events beyond Mr. Obama's control have turned his first and only visit to Spain, the largest European country that had yet to welcome the president, into a rushed one.

Instead of spending two days sightseeing in southern Spain and tending to more official business in the capital of Madrid, the White House scrapped some of Mr. Obama's events - including a staple of his foreign travels, a question-and-answer forum with young adults - and crammed the rest of his schedule into Sunday. He had been scheduled to travel to Seville for cultural events, but that was among the plans canceled.

In a statement announcing the change of plans, the White House said the president had accepted an invitation from Dallas Mayor Mike Rawlings to visit.

Get Breaking News Delivered to Your Inbox

"Later in the week, at the White House, the President will continue to work to bring people together to support our police officers and communities, and find common ground by discussing policy ideas for addressing the persistent racial disparities in our criminal justice system," the statement read.

Deadly shootings last week of black men by police in Louisiana and Minnesota, followed by the sniper killings of five police officers in Dallas, the president to make the unusual choice to return to the White House late Sunday, a day earlier than originally planned.

Mr. Obama has been loath to tear up his schedule in response to previous acts of violence, saying repeatedly that altering his plans would be tantamount to giving in to terrorists. But terrorists didn't strike in Baton Rouge, Louisiana, near St. Paul, Minnesota, or Dallas. The assaults followed June's deadly shooting at an Orlando, Florida, nightclub and the rise in so-called lone-wolf terrorism, heightening fears about public safety.

The White House said Sunday that President Barack Obama will travel to Dallas on Tuesday and deliver remarks at an interfaith memorial service.

The service will take place at the Morton H. Meyerson Symphony Center. The White House says Obama is making the trip at the invitation of Dallas Mayor Mike Rawlings.

The president noted the "difficult week" as he made small talk Sunday with King Felipe VI after arriving at Spain's Royal Palace for a meeting.

The king thanked Mr. Obama for visiting under the circumstances. The stop in Spain was the last leg of what is likely the president's final trip to Europe before he leaves office in January. He arrived late Saturday from Poland, where he attended a NATO summit.

The visit highlighted security cooperation between the trans-Atlantic allies, as well as political and economic ties.

Mr. Obama met with acting Prime Minister Mariano Rajoy before heading to Naval Station Rota, in the south. He toured the USS Ross, one of four guided-missile destroyers based there, and addressed troops there before the flight home.

He told members of the U.S. military in Spain that America will overcome various threats and challenges not only through its military strength but by staying true to its values, including respect for one another and the refusal to be divided by ethnicity and religion.

Mr. Obama said in the face of humanitarian crises, the U.S. and its NATO allies will deliver health and hope to those in need. In the face of aggression, they will stand for the sovereignty of nations such as Ukraine.

Obama said that despite the threats America faces, he's confident that people of goodwill will ultimately overcome forces that seek to "divide and destroy us."

Spain has been gripped by a political stalemate for months, with Rajoy unable to garner the necessary support to form a new coalition government following a late-June election. It was the country's second round of inconclusive balloting in the past year.

Rajoy's party also won an election in December, but no other major party was willing to help him form a government.

Mr. Obama addressed the political situation in an interview Saturday with the El Pais newspaper, saying he hopes Spain's next government will be just as committed to strong relations with the U.S. and Europe. The president has recently stressed the White House's belief in a stronger European Union in the wake of the Brexit vote in the United Kingdom.

In Spain, Mr. Obama said the world needs Spain's contributions to the campaign against the Islamic State of Iraq and Syria (ISIS,) to counterterrorism efforts that prevent attacks and to its fellow NATO members.

"The relationship between Spaniards and Americans goes back centuries," he said. "We're connected by the ties of family and culture, including millions of Americans who celebrate their Hispanic heritage. Spain is a strong NATO ally, we're grateful for Spain's many decades of hosting U.S. forces, and we're major trading partners."

"That's why the United States is deeply committed to maintaining our relationship with a strong, unified Spain," the president said.

Mr. Obama said he has longed to return to Spain ever since he passed through while backpacking across Europe decades ago, during his 20s, a point he underscored just before his private meeting with the king. He said he could not have imagined that he'd return years later and be greeted by royalty.

"I wish I was staying longer," Mr. Obama said Sunday.