Five officers were killed in the ambush on Thursday, making it the deadliest attack on law enforcement since the September 11 terror strike.
Sterling, 37, was fatally shot on July 5 by two Baton Rouge police officers after being tackled to the ground.
Tip-toeing around the racial tensions roiling across the U.S., Obama on Sunday tailored his message directly to activists within Black Lives Matter social movement, cautioning against heated rhetoric and actions aimed at law enforcement.
"We have had a hard week back in the United States, so my trip is a little abbreviated but I thought it was very important for me to come here, given the extraordinary friendship and alliance between Spain and the US", Obama said in a joint press conference with the king Sunday.
But after canceling a state luncheon and a town hall with Spanish youth, much of Obama's visit had the feel of a hurried social call.
Just as protesters need to be respectful of police, the law enforcement community needs to listen the frustrations of people in minority communities, Obama said, and "Not just dismiss these protests and these complaints as political correctness or as politics or attacks on police.".
America has been rocked by the deaths of a series of black men at the hands of white officers-with numerous events captured on camera.
But, he added, "even rhetorically, if we paint police in broad brush without recognizing that the vast majority of police officers are doing a really good job and are trying to protect people and do so fairly and without racial balance, if the rhetoric does not recognize that, then we're going to lose allies in the reform cause".
Obama said he believed an integrated Europe was "one of the greatest political and economic achievements of modern times", but warned it should not be taken for granted as "the project of European integration is being tested like never before". He was invited to speak by Mayor Mike Rawlings, the White House said.
President Obama visited with King Felipe VI of Spain at the Royal Palace in Madrid on Sunday, despite shortening his Europe trip in wake of the recent shootings. He traveled to Spain after attending a North Atlantic Treaty Organisation summit in Poland, but his European visit was overshadowed in many respects by racial and police issues at home. "Spain is a strong North Atlantic Treaty Organisation ally, we're grateful for Spain's many decades of hosting USA forces, and we're major trading partners". Tourists and curiosity seekers lined some streets in hopes of catching a glimpse of him, and local TV aired wall-to-wall coverage of his movements.
He's slated to address US troops and to tour the USS Ross, which is one of four destroyers based in Rota that launch guided missiles.
Shortly before departing for the base, the White House said Obama met with leaders of Spain's main opposition parties. "We've had a hard week back in the United States so my trip is a little abbreviated".
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.
He 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.
"Spain", he said, "has travelled a particularly hard path in recent years".
Superville reported from Seville, Spain.