NEW YORK – The Boston Celtics were so disgusted by their effort that their own coach said they deserved to lose.

Maybe they can take a lesson from Earl Barron.

Barron outworked Boston’s frustrated front line all night, Danilo Gallinari banked in the go-ahead jumper with 36 seconds left and scored a career-high 31 points, and the New York Knicks beat the Celtics 104-101 on Tuesday.

“We didn’t execute. It was simple as that. I thought we just showed up tonight and played basketball, and we had no focus. We deserved to lose,” Boston Coach Doc Rivers said. “The bank shot that Gallinari hit, that was the basketball gods punishing us for not having great focus. You let them hang around, bank shots happen. I just thought we absolutely deserved that.”

Barron had 17 points and a career-best 18 rebounds in his first NBA start in two years after spending most of this season in the NBA Development League.

“I’ve been in the D-League the whole year and been waiting for the opportunity,” Barron said. “And opportunity doesn’t knock many times, so I’m just trying to take advantage of it while I can.”

David Lee had the clinching basket and added 13 points and 11 rebounds for the Knicks, who won their second straight and temporarily avoided a third consecutive 50-loss season.

Ray Allen scored 17 points for the Celtics, who blew a chance to tie Atlanta for third place in the Eastern Conference. Kevin Garnett and Kendrick Perkins each added 14 for Boston, which botched its final possession and couldn’t get a tying 3-point attempt off in the final seconds.

The Celtics followed their victory over Cleveland on Sunday by losing for the fourth time in five games after leading early in the fourth quarter behind a spark from former Knicks guard Nate Robinson. They still believe they’ll put it together in the playoffs, but in the meantime they can’t waste games against teams they should beat.

“We’ve got to take everybody and respect each team in this league,” Garnett said. “You’ve got to come to play every night. You can’t pick and choose who you want to play.”

Gallinari scored 19 points in the third quarter to give the Knicks an 86-78 lead, but Robinson hit a 3-pointer and assisted on Glen Davis’ jumper in the Celtics’ 10-0 run to start the fourth that gave them an 88-86 advantage on Michael Finley’s 3-pointer.

Robinson hit another jumper for a one-point lead midway through the period, and it stayed tight from there. Gallinari banked in a long jumper — originally called a 3-pointer but overturned after replay — for a 102-101 lead.

Chris Duhon came up with a steal and set up Lee’s basket that made it 104-101 with 7.3 seconds to play. The Celtics called time but took way too long to swing the ball to the opposite side of the court, where Rasheed Wallace caught it too late to get off a shot.

With Tracy McGrady (sore left knee) and Al Harrington (sore left ankle) joining Wilson Chandler (sore left groin) on the injured list, the Knicks had to look deep down the bench for scoring. They found it in Barron, a 7-foot center who signed last week and was playing his second game with New York. He shot 8 of 13 from the field.

“I said on the bench that he plays like Antonio McDyess, a very, very good 15-foot jump shooter,” Lee said.