-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42-maximum-depth-v1.html
More file actions
262 lines (222 loc) Β· 9.95 KB
/
42-maximum-depth-v1.html
File metadata and controls
262 lines (222 loc) Β· 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Q42: Maximum Depth of Binary Tree - BFS</title>
<link rel="stylesheet" href="visual.css">
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<div class="container">
<div class="problem-info">
<h1>Q42: Maximum Depth of Binary Tree</h1>
<p>Find the maximum depth (height) of a binary tree using BFS level-by-level traversal.</p>
<div class="problem-meta">
<span class="meta-tag">π Tree</span>
<span class="meta-tag">β±οΈ O(n)</span>
<span class="meta-tag">π‘ BFS Level Order</span>
</div>
</div>
<div class="visualization-section">
<h3>π¬ Visualization</h3>
<div class="controls">
<button class="btn btn-primary" id="stepBtn" onclick="step()">Step</button>
<button class="btn btn-success" id="autoBtn" onclick="toggleAuto()">Auto Run</button>
<button class="btn btn-warning" onclick="reset()">Reset</button>
</div>
<div class="legend">
<div class="legend-item"><div class="legend-circle bg-cyan"></div> Unvisited</div>
<div class="legend-item"><div class="legend-circle bg-orange-deep"></div> Current Level</div>
<div class="legend-item"><div class="legend-circle bg-lime"></div> Visited</div>
</div>
<div class="status-message" id="statusMessage">
Tree: [3, 9, 20, 4, 6, 15, 7]. Click Step to start BFS traversal.
</div>
<div class="info-section">
<div class="info-box highlight">
<div class="info-label">Current Depth</div>
<div class="info-value" id="depthValue">0</div>
</div>
</div>
<div class="tree-container">
<svg id="tree" width="600" height="300"></svg>
</div>
<div class="queue-section">
<div class="queue-title">π Queue (nodes at current level)</div>
<div class="queue-items" id="queueDisplay"><span class="text-gray">Empty</span></div>
</div>
</div>
</div>
<div class="code-section">
<h3>π Python Solution (42-1.py)</h3>
<pre><span class="keyword">import</span> collections
<span class="comment"># Definition for a binary tree node.</span>
<span class="keyword">class</span> TreeNode:
<span class="keyword">def</span> <span class="function">__init__</span>(<span class="keyword">self</span>, x):
<span class="keyword">self</span>.val = x
<span class="keyword">self</span>.left = <span class="keyword">None</span>
<span class="keyword">self</span>.right = <span class="keyword">None</span>
<span class="keyword">class</span> Solution:
<span class="keyword">def</span> <span class="function">maxDepth</span>(<span class="keyword">self</span>, root: TreeNode) -> int:
<span class="keyword">if</span> root is <span class="keyword">None</span>:
<span class="keyword">return</span> <span class="number">0</span>
queue = collections.<span class="function">deque</span>([root])
depth = <span class="number">0</span>
<span class="keyword">while</span> queue:
depth += <span class="number">1</span>
<span class="comment"># ν μ°μ° μΆμΆ λ
Έλμ μμ λ
Έλ μ½μ
</span>
<span class="keyword">for</span> _ <span class="keyword">in</span> <span class="function">range</span>(<span class="function">len</span>(queue)):
cur_root = queue.<span class="function">popleft</span>()
<span class="keyword">if</span> cur_root.left:
queue.<span class="function">append</span>(cur_root.left)
<span class="keyword">if</span> cur_root.right:
queue.<span class="function">append</span>(cur_root.right)
<span class="comment"># BFS λ°λ³΅ νμ == κΉμ΄</span>
<span class="keyword">return</span> depth</pre>
</div>
</div>
<script>
const treeData = {
val: 3,
left: { val: 9, left: { val: 4 }, right: { val: 6 } },
right: { val: 20, left: { val: 15 }, right: { val: 7 } }
};
let nodes = [];
let links = [];
let queue = [];
let depth = 0;
let currentLevel = [];
let visited = new Set();
let done = false;
let autoInterval = null;
function buildTree(node, x, y, dx, depth = 0, parent = null) {
if (!node) return;
const nodeData = { val: node.val, x, y, depth, id: nodes.length };
nodes.push(nodeData);
if (parent !== null) {
links.push({ source: parent, target: nodeData });
}
if (node.left) buildTree(node.left, x - dx, y + 70, dx / 2, depth + 1, nodeData);
if (node.right) buildTree(node.right, x + dx, y + 70, dx / 2, depth + 1, nodeData);
}
function init() {
nodes = [];
links = [];
queue = [];
currentLevel = [];
visited = new Set();
depth = 0;
done = false;
buildTree(treeData, 300, 50, 120);
queue = [nodes[0]];
renderTree();
renderQueue();
}
function renderTree() {
const svg = d3.select("#tree");
svg.selectAll("*").remove();
// Draw links
links.forEach(link => {
svg.append("line")
.attr("x1", link.source.x)
.attr("y1", link.source.y)
.attr("x2", link.target.x)
.attr("y2", link.target.y)
.attr("stroke", "#999")
.attr("stroke-width", 2);
});
// Draw nodes
nodes.forEach(node => {
let fill = "#3498db";
if (visited.has(node.id)) fill = "#27ae60";
if (currentLevel.includes(node.id)) fill = "#f39c12";
svg.append("circle")
.attr("cx", node.x)
.attr("cy", node.y)
.attr("r", 25)
.attr("fill", fill)
.attr("stroke", "#333")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", node.x)
.attr("y", node.y + 5)
.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("font-weight", "bold")
.attr("font-size", "14px")
.text(node.val);
});
}
function renderQueue() {
const container = document.getElementById('queueDisplay');
if (queue.length === 0) {
container.innerHTML = '<span class="text-gray">Empty</span>';
} else {
container.innerHTML = queue.map(n =>
`<div class="queue-item">${n.val}</div>`
).join('');
}
}
function step() {
if (done) {
document.getElementById('stepBtn').disabled = true;
stopAuto();
return;
}
if (queue.length === 0) {
done = true;
currentLevel = [];
document.getElementById('statusMessage').textContent =
`β
BFS Complete! Maximum Depth = ${depth}`;
document.getElementById('stepBtn').disabled = true;
stopAuto();
renderTree();
return;
}
// Process entire level
depth++;
document.getElementById('depthValue').textContent = depth;
const levelSize = queue.length;
currentLevel = queue.map(n => n.id);
const levelVals = queue.map(n => n.val);
// Mark all as visited and collect children
const nextQueue = [];
queue.forEach(node => {
visited.add(node.id);
// Find children
links.forEach(link => {
if (link.source.id === node.id) {
nextQueue.push(link.target);
}
});
});
queue = nextQueue;
document.getElementById('statusMessage').textContent =
`Level ${depth}: Processed nodes [${levelVals.join(', ')}]. Added ${nextQueue.length} children.`;
renderTree();
renderQueue();
}
function toggleAuto() {
if (autoInterval) { stopAuto(); }
else {
document.getElementById('autoBtn').textContent = 'Pause';
autoInterval = setInterval(step, 1000);
}
}
function stopAuto() {
if (autoInterval) { clearInterval(autoInterval); autoInterval = null; }
document.getElementById('autoBtn').textContent = 'Auto Run';
}
function reset() {
stopAuto();
document.getElementById('stepBtn').disabled = false;
document.getElementById('statusMessage').textContent =
'Tree: [3, 9, 20, 4, 6, 15, 7]. Click Step to start BFS traversal.';
document.getElementById('depthValue').textContent = '0';
init();
}
init();
</script>
</body>
</html>