Skip to content

Commit 757363e

Browse files
committed
docs(105): update implementation guides and React README for Construct Binary Tree from Preorder and Inorder Traversal
1 parent c52313a commit 757363e

6 files changed

Lines changed: 36 additions & 16 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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,26 @@ Python の `list.index()` メソッドは内部が C 実装で高速に見えま
9393
チームで長期間メンテナンスするプロダクションコードに向きます。エラーの原因が分かりやすく、pylance による静的型チェックも通る構造になっています。再帰制限の緩和理由もコメントで明示し、後から読んだ人が意図を理解できるようにします。
9494

9595
```python
96+
from __future__ import annotations # アノテーションを遅延評価にする(PEP 563)
97+
9698
import sys
97-
from typing import Optional
99+
from typing import TYPE_CHECKING, Optional
100+
101+
# LeetCode 環境外でも import/実行できるよう最小限の TreeNode を定義する。
102+
# LeetCode では実行時に TreeNode が注入されるため、NameError にならない場合はスキップ。
103+
try:
104+
TreeNode # type: ignore[name-defined]
105+
except NameError:
106+
class TreeNode: # type: ignore[no-redef]
107+
def __init__(
108+
self,
109+
val: int = 0,
110+
left: Optional[TreeNode] = None,
111+
right: Optional[TreeNode] = None,
112+
) -> None:
113+
self.val = val
114+
self.left = left
115+
self.right = right
98116

99117
# 再帰深度の上限を緩和する。
100118
# Python デフォルトは 1000 だが、本問は最悪 3000 段(偏った木)になりうる。

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ impl Solution {
174174
) -> Option<Rc<RefCell<TreeNode>>> {
175175

176176
// ⑥ 再帰の終了条件:left > right なら部分木は空 → None を返す。
177-
// usize は負の数を表せないため、left=0 かつ right=0 のときに
178-
// right - 1 を計算するとアンダーフロー(=0未満へのラップアラウンド)が起きる。
179-
// そのため比較は left > right ではなく、呼び出し側で
180-
// mid == 0 の場合は左再帰を省略するガードが必要になる(⑩で対応)。
177+
// この `if left > right` チェックは Self::build 内で行う(ここ)。
178+
// ただし usize は負の数を表せないため、left=0 かつ mid=0 のとき
179+
// 呼び出し側で `mid - 1` を計算するとアンダーフロー
180+
// (=0未満へのラップアラウンド)が起きる。
181+
// よって両方の対応が必要:
182+
// 1) Self::build 内の `if left > right`(ここ)で空範囲を検出
183+
// 2) 呼び出し側(⑩)の `mid > left` ガードで mid-1 の計算自体を回避
181184
if left > right {
182185
return None;
183186
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def build(lo, hi):
614614
> 📖 **この章で登場した用語**
615615
>
616616
> - **バイトコード**:Python のソースコードが CPython によって変換される中間表現。実際にはこの形式で実行される
617-
> - **dict.**getitem\*\*\*\*`dict[key]` という操作の内部実装。ハッシュ計算で直接位置を求めるため O(1)
617+
> - `dict.__getitem__``dict[key]` という操作の内部実装。ハッシュ計算で直接位置を求めるため O(1)
618618
> - **インタープリタオーバーヘッド**:Python コードの各命令を解釈・実行するためにかかるコスト。C 実装の組み込み関数はこれを大幅に削減できる
619619
> - **不変型(immutable)**`int``str``tuple` など、生成後に値を変更できない型。`+=` は新しいオブジェクトを作る再代入になる
620620
@@ -753,4 +753,3 @@ inorder がそれぞれ `[2,3,1]`, `[1,3,2]`, `[2,1,3]` となり、一意に区
753753

754754
_このドキュメントは LeetCode 105 - Construct Binary Tree from Preorder and Inorder Traversal の解説用に作成されました。_
755755
_対象言語: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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
252252
"""inorder の [lo, hi] 範囲に対応する部分木を再帰構築する"""
253253
nonlocal preorder_idx
254254

255-
# ⑥ 再帰の終了条件: 範囲が空(lo > hi) → 部分木なし
256-
if lo > hi:
255+
# ⑥ 再帰の終了条件: 範囲が空(lo &gt; hi) → 部分木なし
256+
if lo &gt; hi:
257257
return None
258258

259259
# ⑦ preorder の現在位置 = この部分木のルート値
@@ -288,8 +288,8 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
288288
node = TreeNode(3)
289289
node.left = build(0, 0)
290290
root_val=9, preorder_idx→2, mid=0
291-
node.left = build(0,-1) → lo>hi → None
292-
node.right = build(1, 0) → lo>hi → None
291+
node.left = build(0,-1) → lo&gt;hi → None
292+
node.right = build(1, 0) → lo&gt;hi → None
293293
✅ return TreeNode(9)
294294
node.right = build(2, 4)
295295
root_val=20, preorder_idx→3, mid=3

prettier.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ module.exports = {
88
useTabs: false,
99
printWidth: 100,
1010
bracketSpacing: true,
11-
arrowParens: true,
11+
arrowParens: 'always',
1212
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
252252
"""inorder の [lo, hi] 範囲に対応する部分木を再帰構築する"""
253253
nonlocal preorder_idx
254254

255-
# ⑥ 再帰の終了条件: 範囲が空(lo > hi) → 部分木なし
256-
if lo > hi:
255+
# ⑥ 再帰の終了条件: 範囲が空(lo &gt; hi) → 部分木なし
256+
if lo &gt; hi:
257257
return None
258258

259259
# ⑦ preorder の現在位置 = この部分木のルート値
@@ -288,8 +288,8 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
288288
node = TreeNode(3)
289289
node.left = build(0, 0)
290290
root_val=9, preorder_idx→2, mid=0
291-
node.left = build(0,-1) → lo>hi → None
292-
node.right = build(1, 0) → lo>hi → None
291+
node.left = build(0,-1) → lo&gt;hi → None
292+
node.right = build(1, 0) → lo&gt;hi → None
293293
✅ return TreeNode(9)
294294
node.right = build(2, 4)
295295
root_val=20, preorder_idx→3, mid=3

0 commit comments

Comments
 (0)