Binary Tree Maximum Path Sum
Problem (yeniden ifade)
Yol, herhangi bir düğümden düğüme dizidir. Herhangi bir yoldaki düğüm değerlerinin maksimum toplamını döndür (en az bir düğüm).
Sezgi
Her düğümden ebeveyne sunulabilecek en iyi kazanç val + max(0, en iyi çocuk kazancı). Global cevap için her iki çocuğu da kullanan, düğümden geçen yolu da düşün.
Yaklaşımlar
Kazanç DFS + global en iyi
DoğrulanmadıFikir. dfs yukarı kazanç döndürür; globali leftGain+rightGain+val ile güncelle.
Adım adım. Negatif çocuklar max(0, …) ile atılır.
Trade-off’lar. Tek DFS; tümü negatif ağaçlara dikkat (en büyük düğümü seç).
Yardımcı kodu göster
export class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val; this.left = left; this.right = right;
}
}
export class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val; this.left = left; this.right = right;
}
}
type Frame = [TreeNode, number, number]; // node, stage, leftGain
export function maxPathSum(root: TreeNode | null): number {
if (root === null) return 0;
let best = -Infinity;
// Frames live only along the current path, so auxiliary space is O(h).
const stack: Frame[] = [[root, 0, 0]];
let lastGain = 0;
while (stack.length > 0) {
const frame = stack[stack.length - 1];
const node = frame[0];
if (frame[1] === 0) {
frame[1] = 1;
if (node.left !== null) stack.push([node.left, 0, 0]);
else frame[2] = 0;
} else if (frame[1] === 1) {
if (node.left !== null) frame[2] = lastGain;
frame[1] = 2;
if (node.right !== null) stack.push([node.right, 0, 0]);
} else {
const left = frame[2];
const right = node.right !== null ? lastGain : 0;
best = Math.max(best, node.val + left + right);
stack.pop();
lastGain = Math.max(0, node.val + Math.max(left, right));
}
}
return best;
}
type Frame = [TreeNode, number, number]; // node, stage, leftGain
export function maxPathSum(root: TreeNode | null): number {
if (root === null) return 0;
let best = -Infinity;
// Frames live only along the current path, so auxiliary space is O(h).
const stack: Frame[] = [[root, 0, 0]];
let lastGain = 0;
while (stack.length > 0) {
const frame = stack[stack.length - 1];
const node = frame[0];
if (frame[1] === 0) {
frame[1] = 1;
if (node.left !== null) stack.push([node.left, 0, 0]);
else frame[2] = 0;
} else if (frame[1] === 1) {
if (node.left !== null) frame[2] = lastGain;
frame[1] = 2;
if (node.right !== null) stack.push([node.right, 0, 0]);
} else {
const left = frame[2];
const right = node.right !== null ? lastGain : 0;
best = Math.max(best, node.val + left + right);
stack.pop();
lastGain = Math.max(0, node.val + Math.max(left, right));
}
}
return best;
}
Şablon bağlantısı
Yukarı dönen değer val + max(leftGain, rightGain, 0). Düğümden kıvrılan yol, her kazancı 0’da tabanlanmış val + leftGain + rightGain, global en iyiyi günceller. Ebeveyn tek bir aşağı zincir alır.
Notlar
Bir yol, her iki çocuğu da kullanarak bir düğümde bükülebilir; ancak ebeveyne dönen değer yalnızca bir tarafı (veya hiçbiri) içerebilir. Her DFS çağrısı:
leftGain = max(0, dfs(left))ve benzer şekilderightGainhesaplar (negatif kazançları atar).- Global en iyiyi
val + leftGain + rightGainile günceller (bu düğümden geçen yol). val + max(leftGain, rightGain)döndürür (yukarı en iyi zincir).
Tümü negatif ağaçlar: en iyi yol en büyük tek düğümdür (0 kazanç çocuk eklemez).
Yansıma
- Ebeveyne tek kol döner:
val + max(0, sol, sağ). İki kol birden yukarı çıkamaz. - Global en iyi
val + sol kazanımı + sağ kazanımı. Negatif kol 0 sayılır. - Hepsi negatif: en büyük tek düğüm. Çocuğu eklemek yolu küçültüyorsa ekleme.