Norodom Sihanouk, the flamboyant Cambodian monarch whose intermittent rule was marked by shifting alliances, decades of strife and the near-destruction of his country, died Oct. 15 in a Beijing hospital after a heart attack. He was 89 and had battled cancer and other ailments in recent years.

China’s state-run Xinhua news agency reported the death, citing Cambodian Deputy Prime Minister Nhik Bun Chhay. It said Cambodian authorities would bring Sihanouk’s body back to his homeland for a royal funeral.

One of the great survivors of modern Asian politics, Sihanouk served in a variety of leadership roles — both real and titular — in the Cambodian capital, Phnom Penh, and in various governments in exile over seven decades. He twice was crowned king and twice abdicated. He also wielded power as a sovereign prince, a prime minister (serving 10 terms) and a head of state.

As war raged in neighboring Vietnam in the 1960s, Sihanouk struggled to keep his country out of the fighting. But his policy of accommodating the North Vietnamese communists alienated his army, and he was overthrown in 1970 in a rightist coup led by a U.S.-backed general, Lon Nol.

In response to that betrayal, Sihanouk sided with the radical Cambodian communist guerrillas he had dubbed the Khmer Rouge. (The Khmer people are Cambodia’s predominant ethnic group.)

His support gave the Khmer Rouge legitimacy in the eyes of many Cambodians, and it facilitated the movement’s eventual victory, which resulted in a four-year reign of terror that claimed an estimated 1.7 million lives.

That legacy produced the harshest indictment leveled against Sihanouk: that his impulse to settle personal scores nearly ruined his country and made him complicit in the Khmer Rouge holocaust.

“It is beyond question that Sihanouk deeply loved the Cambodian people,” wrote Bruce Sharp, a longtime Cambodia observer and founder of a Web site about Indochina, in a review of Sihanouk’s memoirs. “But Sihanouk had one critical flaw: as much as he loved the Cambodian people, he loved himself just slightly more. At a pivotal moment in Cambodian history, he chose his own interests above those of Cambodia, and millions of people paid with their lives.”

Held as a virtual prisoner in his palace during the Khmer Rouge rule, Sihanouk was largely isolated from the excesses. “I did not see the killing,” he told the New York Times in 1982, “but I saw the forced labor of my people.”

At the time of his death, Sihanouk was the self-styled “king-father” of Cambodia, a title he adopted after abdicating as constitutional monarch in October 2004 on grounds of poor health.

He handed the throne to his youngest surviving son, Norodom Sihamoni, now 59, a ballet instructor, choreographer and diplomat who had lived in France for nearly 20 years.

Although widely revered in Cambodia as a god-king, Sihanouk was well known for his human shortcomings. He was so erratic and temperamental that the adjective “mercurial” became a staple of news stories about him.

He could also be vain, egotistical, self-indulgent, divisive, hypersensitive to perceived slights and given to bouts of gloom and self-pity. But he always stressed that, whatever his faults, there was no doubt about his dedication to his country, its people and its independence.

Born Oct. 31, 1922, in Phnom Penh, Sihanouk was placed on the throne in 1941 by Cambodia’s French colonial rulers after the death of King Sisowath Monivong, his maternal grandfather.

The eldest son of Prince Norodom Suramarit and Princess Sisowath Kossamak, Sihanouk claimed descent from at least 80 Cambodian kings. Before his coronation, he studied at a secondary school in Saigon. He later attended a military school in Saumur, France.

Acting on behalf of the Nazi-controlled Vichy government in collaboration with Japanese military occupation authorities, the French chose Sihanouk over Monivong’s son because they believed the young playboy prince would be malleable. But he proved them wrong, launching a “royal crusade for independence” from France.

It was during that campaign that Sihanouk made his first visit to Washington, but his 1953 meeting with Secretary of State John Foster Dulles left him bitter toward the United States. As he later told journalist Oriana Fallaci, Dulles dismissed his case for independence by telling him, “Go home, your majesty, and thank God that you have the French.”

Sihanouk nevertheless persevered, at one point going into self-imposed exile in neighboring Thailand, and he obtained independence for Cambodia in November 1953.

On the eve of the French departure from his country, Sihanouk recounted in a 1973 memoir, a staff officer whispered to Gen. Paul Girot de Langlade, the French commander in Cambodia: “The king is mad! He expels us from Cambodia, but without us he will be crushed” by the Vietnamese communists. According to Sihanouk, the general turned to the aide and other officers and replied, “Gentlemen, the king may be mad, but it is a brilliant sort of madness.”

In a move to enhance his status as a national leader, Sihanouk, in 1955, abdicated the throne in favor of his father, ending a 14-year reign as king. He set up a national political movement through which he dominated Cambodian politics.

He initially served as prime minister and foreign minister, and in 1956, he even acted as Cambodia’s representative to the United Nations. He established diplomatic relations with Western nations, the Soviet Union and China while seeking aid from both sides of the Cold War.

When his father died in 1960, Sihanouk opted not to succeed him, instead taking over as head of state even as he kept the title of prince and maintained the trappings of the court. He tried to remain neutral during the Cold War in keeping with his dictum, “When two elephants are fighting, an ant should step aside.” But he ultimately tilted toward China, which he believed would eventually dominate Southeast Asia.

As the war in neighboring Vietnam escalated in the spring of 1965, Sihanouk reached a secret agreement with China and North Vietnam to allow the Vietnamese communists to set up bases in eastern Cambodia and receive Chinese military supplies through Cambodian ports.

About the same time, he cut off diplomatic relations with the United States in response to incursions into Cambodia by U.S.-backed South Vietnamese forces. The four-year hiatus in relations was interrupted briefly in November 1967 when Sihanouk hosted former first lady Jacqueline Kennedy, escorting her on a tour of the ancient Angkor Wat temple complex.

While presiding over an autocratic regime that repressed or killed opponents, rigged elections and pilfered public funds, Sihanouk remained generally popular, especially among Cambodia’s peasants. “He personally was honest . . . but he did little to discourage corruption,” historian David P. Chandler wrote in the 1991 book “The Tragedy of Cambodian History.”

Cultured, capable of great charm and fluent in French and English as well as his native Khmer, Sihanouk gained a reputation as a ladies’ man, a gastronome and an extravagant spender. In addition to politics, he dabbled in acting, filmmaking and songwriting. One of his films, “Shadow over Angkor” (1968), portrayed him as thwarting a U.S.-sponsored plot to topple his government.

During the 1940s and ’50s, Sihanouk took at least seven wives or consorts and fathered at least 14 children by five of them, according to a royal family tree. Several of the relationships overlapped. Among the mothers of his children were two of his aunts, both princesses. During his liaisons with them, Sihanouk also married a cousin. Six of his children survive.

Sihanouk’s final marriage — unofficially in 1952 and more formally in 1955 — was to Paule Monique Izzi, now 76, the daughter of a Cambodian woman and a French banker of Italian descent.

She first caught the royal eye at age 15, when Sihanouk awarded her a prize in a Phnom Penh beauty pageant in 1951. She eventually became his principal adviser and wielded great behind-the-scenes influence, palace insiders said.

In 1970, during a visit to Moscow, Sihanouk was deposed by his prime minister, Gen. Lon Nol, and his cousin, Prince Sisowath Sirik Matak. For the proud Sihanouk, the ouster was a humiliation that obsessed him for decades.

From exile, Sihanouk joined forces with the Khmer Rouge, led by Pol Pot, a move that helped the Maoist guerrilla movement gain followers and facilitated its eventual defeat of the U.S.-backed government in April 1975.

But after bringing him back to Phnom Penh, the Khmer Rouge confined Sihanouk to his palace and killed five of his children, at least 14 grandchildren and numerous other relatives and friends during the group’s brutal and destructive rule.

Nevertheless, after a Vietnamese invasion toppled the Khmer Rouge in 1979 and installed a client government, Sihanouk, who had been flown out of Phnom Penh shortly before it fell, again sided with the Khmer Rouge to oppose the foreign occupation of his homeland.

He spent most of his time in Beijing, where he occupied a large government-donated compound near Tiananmen Square, or in North Korea, where his longtime friend Kim Il Sung built him a 60-room palace overlooking a lake north of Pyongyang. But he occasionally visited royalist guerrillas at camps along the Thai-Cambodian border to encourage them in their fight against the Vietnamese occupation, a struggle backed by the United States and China.

Explaining his decision to align himself with the Khmer Rouge, Sihanouk told The Washington Post in a 1985 interview in Thailand: “I have only one nightmare about the future of Cambodia, that it could be ‘Vietnamized’ and lost to the Cambodians, that it could become a second South Vietnam.”

After helping to negotiate a U.N. peace agreement in 1991, Sihanouk returned to his spruced-up palace in Phnom Penh and accepted the title of head of state from the Vietnamese-installed regime he had opposed.

Weeping tears of joy, he greeted thousands of his flag-waving countrymen in the government-organized welcome. Two days later, he publicly repudiated his alliance with the Khmer Rouge, declaring his support for putting Pol Pot and two top lieutenants on trial.

“The reversal of alliances is a common historical phenomenon, and it would be naive for a politician to become indignant over it,” he later wrote.

Less than 10 days after U.N.-supervised elections in 1993, Sihanouk orchestrated one of the erratic maneuvers for which he was renowned. While the votes were still being counted, he declared himself president, prime minister and military supreme commander of a new transitional government, only to renounce the move hours later in response to criticism that it amounted to a coup against the U.N. peace plan.

Cambodia subsequently adopted a constitution that restored the monarchy, allowing Sihanouk, then 70, to resume his reign as king 52 years after he first ascended the throne. Accepting the role of constitutional monarch with limited powers, he vowed to be a king who “reigns but does not govern.”

But he presided over a dysfunctional royal family that insiders described as riven by strife and intrigue. Chief among the rivals were two of his sons: Prince Norodom Ranariddh and Prince Norodom Chakrapong, half brothers on opposite sides of Cambodia’s political divide. Ranariddh, 68, is one of two children Sihanouk fathered by a star of Cambodia’s royal ballet. Chakrapong, 66, is one of seven children from Sihanouk’s marriage to an aunt. The eldest survivor from that union, Prince Norodom Yuvaneath, 68, lives in obscurity in the United States.

Norodom Sihamoni, the surviving son from Sihanouk’s marriage to the woman now known as Queen Norodom Monineath Sihanouk (or, more informally, Queen Monique), was elevated to king in October 2004 when Sihanouk, citing ill health, abdicated for the second time.

Sihanouk then adopted the title “king-father” of Cambodia and divided his time among Beijing, Pyongyang and a palace in Siem Reap in northwestern Cambodia.

His aides maintained a popular royal Web site, on which Sihanouk regularly posted messages, correspondence, cinema commentaries, recipes and other assorted writings — mostly in French.

Other postings on his Web site exuded melancholy, but with his customary courtliness.

In one note in French dating from October 2009 in Beijing, Sihanouk said he found himself thinking of his late mother and praying for his own death, “although the hospitality of the PRC [People’s Republic of China] is exemplary in all respects.”

As his 87th birthday approached, he complained that he had already “lived too much” and that “this too lengthy longevity weighs on me like an unbearable weight.” He thanked “well-meaning compatriots” for wishing he would live to 100. “But to be frank . . . I would like to make known to all, Khmers and foreigners, that they do not make me happy when they wish me long life,” Sihanouk wrote. “What I want is to die as soon as possible, without, however . . . violating the teaching of the august Buddha, who forbids suicide.”