Skip to content

Commit f812ee7

Browse files
committed
fix(docs): resolve markdownlint warnings and update logic in 105
- Normalize heading levels and spacing - Add language specifiers to fenced code blocks - Correct Python logic regarding nonlocal cursor - Fix HTML rendering issues in React component - Restore Prettier config to use tabWidth: 4
1 parent cc8f748 commit f812ee7

7 files changed

Lines changed: 76 additions & 49 deletions

File tree

Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Python.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Python の `list.index()` メソッドは内部が C 実装で高速に見えま
7474

7575
## コードの骨格(先に全体像を把握する)
7676

77-
```
77+
```text
7878
1. 入力検証: 長さ不一致・空リストを早期検出
7979
2. 前処理: inorder の「値 → インデックス」dict を O(n) で構築
8080
3. preorder カーソル idx を nonlocal で再帰間共有する準備
@@ -240,7 +240,7 @@ class Solution:
240240

241241
## 動作トレース(具体的な入力例)
242242

243-
```
243+
```text
244244
入力: preorder = [3, 9, 20, 15, 7]
245245
inorder = [9, 3, 15, 20, 7]
246246

Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Rust.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ TypeScript 版との最大の違いは、**ノードの型が `Option<Rc<RefCell
6464

6565
- **選択したアプローチ**: **B: 再帰 + HashMap + `&mut usize`**
6666

67-
### 理由
67+
## 理由
6868

6969
- **A を選ばなかった理由**: ループのたびに `preorder[1..]``inorder[..mid]``Vec` のコピーを生成すると、n 個のノードで合計 O(n²) のメモリアロケーションが発生します。Rust では不要なヒープアロケーションは避けるのが基本設計方針です
7070
- **C を選ばなかった理由**: `Rc<RefCell<TreeNode>>``.borrow_mut()` を手動スタックで管理するとコードが複雑になり、実行時パニック(`borrow_mut` の二重借用)のリスクも高まります
@@ -87,7 +87,7 @@ TypeScript 版との最大の違いは、**ノードの型が `Option<Rc<RefCell
8787

8888
## コードの骨格(先に全体像を把握する)
8989

90-
```
90+
```text
9191
1. 入力検証: preorder と inorder の長さが一致しなければ None を返す
9292
2. 前処理: inorder の「値 → インデックス」を HashMap に O(n) で登録
9393
3. preorder カーソル preorder_idx を 0 で初期化し、&mut で再帰関数に渡す
@@ -230,7 +230,7 @@ impl Solution {
230230

231231
## 動作トレース(具体的な入力例)
232232

233-
```
233+
```text
234234
入力: preorder = [3, 9, 20, 15, 7]
235235
inorder = [9, 3, 15, 20, 7]
236236
@@ -290,7 +290,7 @@ return Some(TreeNode(3, left=Some(9), right=Some(20)))
290290

291291
## `Rc<RefCell<>>` を使う理由を図で理解する
292292

293-
```
293+
```text
294294
【他言語(TypeScript)では】
295295
node.left = buildLeft(); // 普通に代入できる
296296

Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Typescript.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
2つの配列が何を意味するかを理解しないと解けません。木(ツリー)の探索には複数の「順序」があり、それぞれ異なる情報を持っています。
1515

16-
```
1716
【二分木(binary tree)の探索順序の種類】
1817

19-
3 ← これを元の木として考える
18+
```text
19+
3
2020
/ \
2121
9 20
2222
/ \
@@ -78,7 +78,7 @@ inorder(中順)= [9, 3, 15, 20, 7]
7878
> - `O(1)`:入力サイズに関係なく一定時間(最速)
7979
> - `O(n)`:入力が2倍になると処理も約2倍
8080
> - `O(n²)`:入力が2倍になると処理は約4倍(二重ループに多い)
81-
81+
>
8282
> 📖 **このセクションで登場した用語**
8383
>
8484
> - **時間計算量**: 処理にかかる手間が入力サイズに対してどう増えるかの目安
@@ -91,7 +91,7 @@ inorder(中順)= [9, 3, 15, 20, 7]
9191

9292
- **選択したアプローチ**: **B: 再帰 + HashMap(O(n))**
9393

94-
### 理由
94+
## 理由
9595

9696
- **Aを選ばなかった理由**: `indexOf` による毎回の線形探索(=先頭から1つずつ調べる処理)が O(n) かかり、n 個のノードで合計 O(n²) になります。制約 `n=3000` なら9,000,000回の比較が必要になり遅すぎます
9797
- **Cを選ばなかった理由**: スタック(=後入れ先出しのデータ構造)を使った反復実装は速度は同じですが、コードが複雑になり保守性(=後から変更しやすい性質)が下がります
@@ -114,7 +114,7 @@ inorder(中順)= [9, 3, 15, 20, 7]
114114

115115
## コードの骨格(先に全体像を把握する)
116116

117-
```
117+
```text
118118
1. 入力検証(配列長の一致確認・空配列チェック)
119119
2. 前処理:inorder の「値 → インデックス」HashMap を O(n) で構築
120120
3. preorder の現在位置を指す変数を初期化
@@ -215,7 +215,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
215215

216216
## 動作トレース(具体的な入力例)
217217

218-
```
218+
```text
219219
入力: preorder = [3, 9, 20, 15, 7]
220220
inorder = [9, 3, 15, 20, 7]
221221

Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/README.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- **時間計算量**:O(n)(各ノードをちょうど1回だけ処理)
5353
- **空間計算量**:O(n)(`dict` の n エントリ + 再帰スタックの高さ h 分)
5454

55-
```
55+
```text
5656
【2つの配列が持つ情報の整理】
5757
5858
preorder = [3, 9, 20, 15, 7]
@@ -152,7 +152,7 @@ graph LR
152152

153153
入力 `preorder=[3,9,20,15,7]``inorder=[9,3,15,20,7]` を使って各ステップを追います。
154154

155-
```
155+
```text
156156
【前処理】inorder_dict の構築(O(n)):
157157
{ 9:0, 3:1, 15:2, 20:3, 7:4 }
158158
@@ -302,7 +302,7 @@ Output: [3,9,20,null,null,15,7] ✅
302302

303303
> 💡 コードを読む前に、実装の骨格を確認しましょう。
304304
305-
```
305+
```text
306306
実装の骨格:
307307
1. sys.setrecursionlimit で再帰深度の上限を緩和する(最悪 3000 段の再帰に備えるため)
308308
2. 入力検証:型チェック・長さ不一致・空リストを早期検出する
@@ -348,22 +348,18 @@ if TYPE_CHECKING:
348348
right: Optional[TreeNode] = None,
349349
) -> None: ...
350350

351-
# LeetCode の実行環境では TreeNode は既に定義済みのため、
352-
# NameError が出ない場合はスキップ。定義がなければ軽量フォールバックを用意する。
353-
try:
354-
TreeNode # type: ignore[name-defined]
355-
except NameError:
356-
class TreeNode: # type: ignore[no-redef]
357-
__slots__ = ("val", "left", "right")
358-
def __init__(
359-
self,
360-
val: int = 0,
361-
left: Optional[TreeNode] = None,
362-
right: Optional[TreeNode] = None,
363-
) -> None:
364-
self.val = val
365-
self.left = left
366-
self.right = right
351+
# Provide a lightweight TreeNode fallback for environments that do not supply it.
352+
class TreeNode:
353+
__slots__ = ("val", "left", "right")
354+
def __init__(
355+
self,
356+
val: int = 0,
357+
left: Optional["TreeNode"] = None,
358+
right: Optional["TreeNode"] = None,
359+
) -> None:
360+
self.val = val
361+
self.left = left
362+
self.right = right
367363

368364

369365
class Solution:
@@ -501,7 +497,7 @@ class Solution:
501497

502498
### 動作トレース(業務版コードの主要ステップ確認)
503499

504-
```
500+
```text
505501
入力: preorder=[3,9,20,15,7], inorder=[9,3,15,20,7]
506502
507503
Step 1: isinstance チェック → 両方 list ✅
@@ -631,7 +627,7 @@ def build(lo, hi):
631627
| # | ケース名 | 入力 | 期待出力 | なぜ問題になりうるか |
632628
| --- | -------------------- | ---------------------------------- | ------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
633629
| 1 | 単一ノード | `pre=[-1]`, `ino=[-1]` | `TreeNode(-1)` | `build(0,0)``lo==hi` になり終了条件ギリギリ。`lo-1=-1``lo>hi` になるかチェック |
634-
| 2 | 右に偏った木 | `pre=[1,2,3]`, `ino=[1,2,3]` | 1→None→2→None→3 | mid が常に lo と一致するため左再帰が `build(lo,lo-1)` = `build(x,x-1)` になる。`lo-1` のアンダーフローは Python では起きないが lo > hi の判定が正しく機能するかを確認 |
630+
| 2 | 右に偏った木 | `pre=[1,2,3]`, `ino=[1,2,3]` | 1→None→2→None→3 | mid が常に lo と一致するため左再帰が `build(lo,lo-1)` になる。`build(lo, hi)` の終了条件 `lo > hi` が正しく機能するかを確認 |
635631
| 3 | 左に偏った木 | `pre=[3,2,1]`, `ino=[1,2,3]` | 3→2→1→None 右側全部 None | mid が常に hi と一致するため右再帰が `build(hi+1,hi)` = `build(x+1,x)` になる。終了条件が正しく機能するかを確認 |
636632
| 4 | 負の値を含む | `pre=[-3,9,-20]`, `ino=[9,-3,-20]` | `TreeNode(-3, TreeNode(9), TreeNode(-20))` | `dict` のキーに負の値が使えるか確認。Python の `dict` は任意の `int` をキーにできるため問題ない |
637633
| 5 | 最大サイズ | n=3000 の完全二分木 | 正常に木を返す | 再帰深度が約 log₂(3000) ≈ 12 段に収まる。`sys.setrecursionlimit` なしでも動くが念のため設定済み |
@@ -723,7 +719,7 @@ except ValueError:
723719

724720
**理由**:例えば preorder `[1,2,3]` に対して、以下の複数の木が存在します:
725721

726-
```
722+
```text
727723
パターンA パターンB パターンC
728724
1 1 1
729725
/ \ / \
@@ -757,3 +753,4 @@ inorder がそれぞれ `[2,3,1]`, `[1,3,2]`, `[2,1,3]` となり、一意に区
757753

758754
_このドキュメントは LeetCode 105 - Construct Binary Tree from Preorder and Inorder Traversal の解説用に作成されました。_
759755
_対象言語:Python (CPython 3.11.10) / プラットフォーム:LeetCode_
756+
��:Python (CPython 3.11.10) / プラットフォーム:LeetCode_

Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/README_React.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
480480
# 再帰深度の上限を緩和(デフォルト1000 → 偏った木で3000段になりうる)
481481
sys.setrecursionlimit(10_000)
482482

483+
# Provide a lightweight TreeNode fallback for environments that do not supply it.
484+
class TreeNode:
485+
__slots__ = ("val", "left", "right")
486+
def __init__(
487+
self,
488+
val: int = 0,
489+
left: Optional["TreeNode"] = None,
490+
right: Optional["TreeNode"] = None,
491+
) -> None:
492+
self.val = val
493+
self.left = left
494+
self.right = right
483495

484496
class Solution:
485497
def buildTree(
@@ -512,7 +524,7 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
512524
nonlocal preorder_idx # 外側のカーソルを書き換えることをPythonに宣言
513525

514526
# ⑥ 再帰の終了条件:範囲が空 → 部分木なし → None
515-
if lo > hi:
527+
if lo &gt; hi:
516528
return None
517529

518530
# ⑦ preorder の現在位置がこの部分木のルート値
@@ -1911,16 +1923,19 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
19111923
useEffect(() => {
19121924
if (isPlaying) {
19131925
timerRef.current = setTimeout(() => {
1914-
if (activeStep === stepsData.length) {
1915-
setActiveStep(1);
1916-
setIsPlaying(false);
1917-
} else setActiveStep((p) => p + 1);
1926+
setActiveStep((p) => {
1927+
if (p === stepsData.length) {
1928+
setIsPlaying(false);
1929+
return 1;
1930+
}
1931+
return p + 1;
1932+
});
19181933
}, 2400);
19191934
}
19201935
return () => {
19211936
if (timerRef.current) clearTimeout(timerRef.current);
19221937
};
1923-
}, [isPlaying, activeStep]);
1938+
}, [isPlaying]);
19241939
return (
19251940
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
19261941
<div>

prettier.config.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module.exports = {
44
semi: true,
55
singleQuote: true,
66
trailingComma: 'all',
7-
tabWidth: 2,
7+
tabWidth: 4,
88
useTabs: false,
99
printWidth: 100,
1010
bracketSpacing: true,
11-
arrowParens: 'always',
12-
endOfLine: 'lf',
11+
arrowParens: true,
12+
endOfLine: 'auto',
1313
};

public/Algorithm/BinaryTree/claude sonnet 4.6 extended/105. Construct Binary Tree from Preorder and Inorder Traversal/README_React.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
480480
# 再帰深度の上限を緩和(デフォルト1000 → 偏った木で3000段になりうる)
481481
sys.setrecursionlimit(10_000)
482482

483+
# Provide a lightweight TreeNode fallback for environments that do not supply it.
484+
class TreeNode:
485+
__slots__ = ("val", "left", "right")
486+
def __init__(
487+
self,
488+
val: int = 0,
489+
left: Optional["TreeNode"] = None,
490+
right: Optional["TreeNode"] = None,
491+
) -> None:
492+
self.val = val
493+
self.left = left
494+
self.right = right
483495

484496
class Solution:
485497
def buildTree(
@@ -512,7 +524,7 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
512524
nonlocal preorder_idx # 外側のカーソルを書き換えることをPythonに宣言
513525

514526
# ⑥ 再帰の終了条件:範囲が空 → 部分木なし → None
515-
if lo > hi:
527+
if lo &gt; hi:
516528
return None
517529

518530
# ⑦ preorder の現在位置がこの部分木のルート値
@@ -1911,16 +1923,19 @@ <h1 class="outfit text-4xl font-black mb-2 glow-text" style="color: #f0fdf4">
19111923
useEffect(() => {
19121924
if (isPlaying) {
19131925
timerRef.current = setTimeout(() => {
1914-
if (activeStep === stepsData.length) {
1915-
setActiveStep(1);
1916-
setIsPlaying(false);
1917-
} else setActiveStep((p) => p + 1);
1926+
setActiveStep((p) => {
1927+
if (p === stepsData.length) {
1928+
setIsPlaying(false);
1929+
return 1;
1930+
}
1931+
return p + 1;
1932+
});
19181933
}, 2400);
19191934
}
19201935
return () => {
19211936
if (timerRef.current) clearTimeout(timerRef.current);
19221937
};
1923-
}, [isPlaying, activeStep]);
1938+
}, [isPlaying]);
19241939
return (
19251940
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
19261941
<div>

0 commit comments

Comments
 (0)