feat: LeetCode Hot 100 - 100道题完整交互式图解页面
Deploy / deploy (push) Successful in 7s

This commit is contained in:
2026-08-24 04:35:13 +00:00
parent e4fe5afb80
commit 4f830ad352
114 changed files with 32137 additions and 82 deletions
+190
View File
@@ -0,0 +1,190 @@
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>040. 二叉树的直径 – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>
/* page-specific overrides */
.vis-area { min-height: 120px; padding: 16px 0; }
.code-section { margin-top: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>🟢 040. 二叉树的直径 <span class="badge easy">简单</span></h1>
<p class="subtitle">分类:二叉树 | LeetCode Hot 100</p>
<!-- 控制面板 -->
<div class="controls" id="controls">
<label for="inputArea">输入:</label>
<input type="text" id="inputArea" placeholder="默认示例,可自定义">
<button id="applyBtn" class="primary">生成图解</button>
<select id="exampleSelect"></select>
<span style="flex:1"></span>
<button id="prevBtn">◀ 上一步</button>
<button id="nextBtn">下一步 ▶</button>
<button id="jumpBtn">⏭ 跳到结果</button>
<button id="autoBtn">自动播放</button>
<button id="resetBtn">重置</button>
</div>
<!-- 步骤流水线 -->
<div class="pipeline" id="pipeline"></div>
<!-- 提示条 -->
<div class="hint info" id="hintBox">
<span id="stepInfo"></span><br>
<span id="hintText"></span>
</div>
<!-- 可视化面板 -->
<div class="panels">
<div class="panel" id="mainPanel">
<h3>📊 可视化</h3>
<div class="vis-area" id="vizArea">点击「生成图解」开始</div>
</div>
<div class="panel-grid">
<div class="panel" id="detailPanel">
<h3>📝 当前步骤详情</h3>
<div id="detailContent">等待开始...</div>
</div>
<div class="panel" id="resultPanel">
<h3>✅ 结果</h3>
<div id="resultContent">等待完成...</div>
</div>
</div>
</div>
<!-- 代码 -->
<div class="panel code-section">
<h3>💻 参考代码(Python)</h3>
<div id="codeArea"></div>
</div>
<footer>Powered by QwenPaw · 图解算法 · LeetCode Hot 100</footer>
</div>
<script src="../shared/algo-viz.js"></script>
<script>
"use strict";
(function() {
// ========== Algorithm Logic ==========
function arrayToTree(arr) {
if (!arr || arr.length === 0) return null;
let idCtr = 0;
const nodes = arr.map(v => v === null ? null : {val: v, left: null, right: null, _id: 'n' + (idCtr++)});
for (let i = 0; i < nodes.length; i++) {
if (!nodes[i]) continue;
const li = 2*i+1, ri = 2*i+2;
if (li < nodes.length) nodes[i].left = nodes[li];
if (ri < nodes.length) nodes[i].right = nodes[ri];
}
return nodes[0];
}
function treeToArray(root) {
if (!root) return [];
const res = [], q = [root];
while (q.length) {
const n = q.shift();
if (n === null) { res.push(null); continue; }
res.push(n.val); q.push(n.left, n.right);
}
while (res.length && res[res.length-1] === null) res.pop();
return res;
}
function deepClone(node) {
if (!node) return null;
return {val: node.val, _id: node._id, left: deepClone(node.left), right: deepClone(node.right)};
}
const examples = [
{tree: [1,2,3,4,5], label: '示例1: [1,2,3,4,5] 直径=3'},
{tree: [1,2], label: '示例2: [1,2] 直径=1'},
{tree: [1,2,3,4], label: '示例3: [1,2,3,4]'},
];
let root, steps, stepCtrl;
function buildSteps(arr) {
root = arrayToTree(arr);
steps = [];
let maxDiameter = 0;
steps.push({stage:'init',hl:{},msg:'DFS 返回每个节点的深度,维护最大直径(左深度+右深度)',maxDia:0});
function dfs(node) {
if (!node) return 0;
steps.push({stage:'enter',hl:{[node._id]:'current'},msg:`进入节点 ${node.val}`,maxDia:maxDiameter});
const ld = dfs(node.left);
const rd = dfs(node.right);
const diameter = ld + rd;
if (diameter > maxDiameter) maxDiameter = diameter;
const depth = Math.max(ld, rd) + 1;
steps.push({stage:'calc',hl:{[node._id]:'visited'},msg:`节点 ${node.val}:左深=${ld},右深=${rd},直径=${ld}+${rd}=${diameter},最大直径=${maxDiameter},返回深度=${depth}`,maxDia:maxDiameter,depth});
return depth;
}
if (root) dfs(root);
steps.push({stage:'done',hl:{},msg:`最大直径 = ${maxDiameter}`,maxDia:maxDiameter});
}
function render(step) {
const s = steps[step];
let viz = renderBinaryTree(root, {highlights: s.hl});
$('vizArea').innerHTML = viz;
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
if (s.maxDia !== undefined) {
$('detailContent').innerHTML += `<div class="current-answer">当前最大直径:<b>${s.maxDia}</b></div>`;
}
if (s.stage === 'done') {
$('resultContent').innerHTML = `<div class="final-answer">二叉树的直径 = <b>${s.maxDia}</b></div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','enter→进入','calc→计算','done→完成'];
$('pipeline').innerHTML = stages.map(st => {
const [k,l] = st.split('→');
return `<span class="pipe-step ${s.stage===k?'active':''}">${l}</span>`;
}).join('<i>→</i>');
}
function init() {
const sel = $('exampleSelect');
examples.forEach((e,i) => { sel.innerHTML += `<option value="${i}">${e.label}</option>`; });
$('inputArea').value = '[1,2,3,4,5]';
buildSteps(examples[0].tree);
stepCtrl = new StepController({onStep: render});
stepCtrl.setSteps(steps.map((_,i)=>i));
$('stepInfo').textContent = '步骤 1 / ' + steps.length;
stepCtrl.onStep = (idx) => { render(idx); $('stepInfo').textContent = `步骤 ${idx+1} / ${steps.length}`; };
$('applyBtn').onclick = () => {
try { const arr = JSON.parse($('inputArea').value); buildSteps(arr); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length; }
catch(e) { alert('请输入合法数组'); }
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
$('inputArea').value = JSON.stringify(e.tree);
buildSteps(e.tree); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
};
$('prevBtn').onclick = () => stepCtrl.prev();
$('nextBtn').onclick = () => stepCtrl.next();
$('jumpBtn').onclick = () => stepCtrl.jumpToEnd();
$('autoBtn').onclick = () => { const on = stepCtrl.toggleAuto(); $('autoBtn').textContent = on ? '暂停' : '自动播放'; };
$('resetBtn').onclick = () => { stepCtrl.reset(); $('autoBtn').textContent = '自动播放'; };
}
init();
$('codeArea').innerHTML = renderCode(`def diameterOfBinaryTree(root):
max_d = 0
def depth(node):
nonlocal max_d
if not node: return 0
l = depth(node.left)
r = depth(node.right)
max_d = max(max_d, l + r)
return max(l, r) + 1
depth(root)
return max_d`, {lang:'Python'});
})();
</script>
</body>
</html>