The BBC's Alpa Patel: "This attack comes at a tense time in the United States"
A gunman has shot dead two police officers sitting inside a patrol car in New York before killing himself.
The head of the New York police said the men had been "targeted for their uniform". The gunman then ran into a subway station where he shot himself.
Earlier he had shot and injured his ex-girlfriend and had posted anti-police messages on social media.
President Barack Obama - who is on holiday in Hawaii - said he condemned the killings "unconditionally".
"Officers who serve and protect our communities risk their own safety for ours every single day and they deserve our respect and gratitude every single day," he said in a statement.
Liu Wenjin (left) and Raphael Ramos were the police officers killed
The killings come amid widespread dissatisfaction in relations between police and African Americans.
The gunman was a black man - named as Ismaaiyl Brinsley, 28 - while the two police officers, Liu Wenjin and Raphael Ramos, were Asian and Hispanic respectively.
Earlier this month, a grand jury decided not to indict a New York officer for the chokehold death of Eric Garner, a black man who died when white police officers tried to arrest him for selling cigarettes.
Last month, another grand jury also cleared a white officer in the fatal shooting of Michael Brown, an unarmed black 18-year-old, in Ferguson, Missouri.
Both decisions triggered nationwide protests.
New York police commissioner Bill Bratton said his department was looking at whether the suspect had attended any rallies.
Two officials told the Associated Press news agency that the gunman had also posted about shooting police in retaliation for the death of Mr Garner.
The mayor of New York, Bill de Blasio, said anyone seeing postings indicating a threat to the police should report them.
The officers were on duty in the Bedford-Stuyvesant area of Brooklyn when they were shot on Saturday afternoon.
Mr Bratton said the officers had been shot with "no warning, no provocation - they were quite simply assassinated".
There was a heavy police presence in the area following the shooting
Police released a photo of the pistol found near the body of the dead gunman
He also said the suspect had wounded his former girlfriend earlier on Saturday in Baltimore and had made posts from her Instagram account.
"This may be my final post," said one that included an image of a silver handgun.
The Rev Al Sharpton, a prominent civil rights activist, said Mr Garner's family had had no connection to the gunman and he denounced the violence.
"Any use of the names of Eric Garner and Michael Brown in connection with any violence or killing of police, is reprehensible and against the pursuit of justice in both cases," Rev Sharpton said.
The last fatal shooting of a New York police officer was in 2011.
The head of the New York police officers union, Patrick Lynch told the BBC: "There's blood on many hands tonight. Those that incited violence on the street under the guise of protest. We tried to warn - it must not go on, it cannot be tolerated."