The Supreme Court ruled 5 to 4 yesterday that states may not impose a mandatory death sentence on persons convicted of murdering a policeman.

Laws that make no allowance for possible mitigating factors deny the constitutional protection against cruel and unusual punishment, the court held.

The majority recognized a state's "special interest" in protecting police officers "who regularly must risk their lives." The unsigned opinion noted that 129 police officers were killed in line of duty in 1975, compared with 57 in 1966.

But, the opinion said, it "is "incorrect to rule out the possibility in such slayings of any mitigating facts, including "the youth of the offender." In the case decided yesterday, the offender, Harry Roberts, was 19 when he was sentenced to die for killing New Orleans police officer Dennis McInerney in February, 1974.

The majority's list of facts that might mitigate the intentional killing of a peace officer - and that are relevant under existing law in some states - also included:

. . . the absence of any prior conviction, the influence of drugs, alcohol or extreme emotional disturbance, and even the existence of circumstances which the offender reasonably believed provided a moral justification for his conduct . . ."

Dissenting Justice Harry A. Blackmun, joined by Justices Byron R. White and William H. Rehnquist, said he does not read the majority opinion "as one deciding the issue of the constitutionality of a mandatory death sentence for a killer of a peace officer for all cases and all times." Currently, only New York State has a law similar to the Louisiana statute invalidated yesterday.

In a separate dissent joined by White, Rehnquist termed it "astonishing" that the majority could include in its list of possible itigating circumstances those "reasonably believed" by an offender to provide "a moral justification . . ."

"John Wilkes Booth may well have thought he was morally justified in murdering Abraham Lincoln, whom, while fleeing from the stage of Ford's Theater, he characterized as a 'tyrant,'" Rehnquist wrote.

Chief Justice Warren E. Burger wrote a separate, one-sentence dissent.

Two in the majority, Justices William J. Brennan Jr. and Thurgood Marshall, noted their long-standing view that the death penalty is unconstitutional cruel and unusual punishment "in all circumstances." The other justices in the majority were Potter Stewart, Lewis F. Powell Jr. and John Paul Stevens.

Yesterday's decision evolved from a call to New Orleans police on Mardi Gras day , 1974, from persons who said a young man had pulled a pistol, fored three shots, allegedly wounded a child, and fled.

Responding, Scout car officers John Tobin and McInerney saw Roberts and headed him off by driving onto a sidewalk. Roberts ran to the passenger side and began shooting through the open window, hitting Tobin in the head and leg. McInerney emerged from behind the wheel and was fatally shot in the face. Roberts ran but was hit in the left leg by a Tobin bullet. When caught, Roberts asserted innocence and claimed someone else had inflicted his leg wound.

Roberts was sentenced under a 1974 law making the death penalty compulsory for an offender who had "a specific intent to kill or go inflict great bodily harm upon . . . a police officer engaged in the performance of his lawful duties." The state contends "there are obsolutely no mitigating circumstances possible" in such a context.

The highest Louisiana court upheld the conviction. Yesterday's decision reversed it and sent the case back to the state courts for possible retrial.

The Supreme Court took other actions: ONE-HOUSE VETO

Last year, former Attorney General Ramsey Clark, as an unsuccessful candidate for the Democratic Senate nomination in New York, challenged certain Federal Election Commission regulations. One of his major contentions was that the rules were unconstitutional because they are subject to veto by either the House or Senate.

The U.S. Court of Appeals ruled that, because the one-house veto had not been exercised in the Clark case, it was "not ripe" for judicial determination. Even if the issue were ripe, the court said, judicial prudence dictated that the dispute over the veto, which has been under White House attack for a half-century, be resolved by the political process.

After intervening in the appellate court to say the issue was ripe, the Justice Department told the Supreme Court it wasn't. Yesterday, the Supreme Court let the ruling stand, thus preserving the veto until another challenge reaches the justices, possibly in a couple of years. TEXAS ELECTION CONTEST

Last year, uncumbent Rep. Ron Paul (R-Tex.) lost his seat to Robert A. Gammage (D) by a margin of only 298 of the 192,802 votes cast in their Houston district. In May, the House dismissed Paul's challenge of the outcome. Yesterday, the court followed suit. OPENING MAIL

In February, 1974, a customs officer in New York spotted eight bulky air mail envelopes from Thailand, a source of heroin. He opened them and found bags of what proved to be the narcotic. As a result, Charles W. Ramsey and James W. Kelly of the District of Columbia were convicted and sentenced to 10 to 30 years for operating a "heroin-by-mail" enterprise.

The U.S. Court of Appeals here, disagreeing with other appellate courts, upset the convictions, saying that the constitution forbade opening such mail without probable cause and a search warrant.

Yesterday, the Supreme Court reversed 6 to 3 Justice Hehnquist said postal regulations authorize customs officials to open - but not read - mail that they have "reasonable cause to suspect" contains illegally imported merchandise. For the dissenters, Justice Stevens wrote, "If the government is allowed to exercise the power it claims, the door will be open to the wholesale, secret examination of all incoming international letter mail."