File tree Expand file tree Collapse file tree
Algorithm/BinaryTree/claude sonnet 4.6 extended
105. Construct Binary Tree from Preorder and Inorder Traversal
106. Construct Binary Tree from Inorder and Postorder Traversal
Algorithm/BinaryTree/claude sonnet 4.6 extended
105. Construct Binary Tree from Preorder and Inorder Traversal
106. Construct Binary Tree from Inorder and Postorder Traversal Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -218,7 +218,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
218218 self,
219219 preorder: list[int],
220220 inorder: list[int],
221- ) -> Optional[TreeNode]:
221+ ) -> Optional[TreeNode]:
222222 """
223223 preorder(前順)と inorder(中順)から二分木を復元する。
224224 Time: O(n) - 各ノードをちょうど1回処理
@@ -248,7 +248,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
248248 # int はイミュータブル(変更不可)なので nonlocal で外側変数を共有する
249249 preorder_idx: int = 0
250250
251- def build(lo: int, hi: int) -> Optional[TreeNode]:
251+ def build(lo: int, hi: int) -> Optional[TreeNode]:
252252 """inorder の [lo, hi] 範囲に対応する部分木を再帰構築する"""
253253 nonlocal preorder_idx
254254
Original file line number Diff line number Diff line change @@ -346,16 +346,20 @@ return TreeNode(3) ✅
346346# リストで包む(ミュータブルオブジェクトに変換する)ことで回避できる
347347
348348# 方法1: nonlocal を使う(明示的だが宣言が必要)
349- count = 0
350- def inner ():
351- nonlocal count # ← これがないと count += 1 で UnboundLocalError になる
352- count += 1
349+ def outer1 ():
350+ count = 0
351+ def inner ():
352+ nonlocal count # ← これがないと count += 1 で UnboundLocalError になる
353+ count += 1
354+ return inner
353355
354356# 方法2: リストで包む(nonlocal 不要・慣用句として広く使われる)
355- count = [0 ] # int ではなく list[int] にする
356- def inner ():
357- count[0 ] += 1 # リストの中身を書き換えるのは「外側変数の再代入」ではない
358- # → nonlocal 不要
357+ def outer2 ():
358+ count = [0 ] # int ではなく list[int] にする
359+ def inner ():
360+ count[0 ] += 1 # リストの中身を書き換えるのは「外側変数の再代入」ではない
361+ # → nonlocal 不要
362+ return inner
359363```
360364
361365### dict内包表記 vs ` enumerate() ` + ループ
Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
155155 self,
156156 inorder: List[int],
157157 postorder: List[int]
158- ) -> Optional[TreeNode]:
158+ ) -> Optional[TreeNode]:
159159 # Python のデフォルト再帰上限は 1000。
160160 # 完全偏り木では n=3000 段の再帰が必要になるため上限を引き上げる。
161161 sys.setrecursionlimit(10_000)
@@ -169,9 +169,9 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
169169 # リストに包むことで「リストの中身を変える」操作になり nonlocal 不要になる。
170170 post_idx: List[int] = [len(postorder) - 1]
171171
172- def dfs(left: int, right: int) -> Optional[TreeNode]:
172+ def dfs(left: int, right: int) -> Optional[TreeNode]:
173173 # 終了条件:左端が右端を超えた = この範囲に要素がない = 部分木なし
174- if left > right:
174+ if left > right:
175175 return None
176176
177177 # postorder の末尾から現在の部分木のルートを取り出す。
@@ -1066,7 +1066,9 @@ <h3 className="mt-0 text-teal-800 text-base font-bold mb-2">{currentStepData.tit
10661066root . render ( React . createElement ( StepsApp ) ) ;
10671067
10681068// Re-highlight code after React renders
1069- setTimeout ( ( ) => { if ( window . Prism ) Prism . highlightAll ( ) ; } , 300 ) ;
1069+ requestAnimationFrame ( ( ) => {
1070+ if ( window . Prism ) Prism . highlightAllUnder ( document . getElementById ( "react-steps" ) ) ;
1071+ } ) ;
10701072</ script >
10711073</ body >
10721074</ html >
Original file line number Diff line number Diff line change @@ -218,7 +218,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
218218 self,
219219 preorder: list[int],
220220 inorder: list[int],
221- ) -> Optional[TreeNode]:
221+ ) -> Optional[TreeNode]:
222222 """
223223 preorder(前順)と inorder(中順)から二分木を復元する。
224224 Time: O(n) - 各ノードをちょうど1回処理
@@ -248,7 +248,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
248248 # int はイミュータブル(変更不可)なので nonlocal で外側変数を共有する
249249 preorder_idx: int = 0
250250
251- def build(lo: int, hi: int) -> Optional[TreeNode]:
251+ def build(lo: int, hi: int) -> Optional[TreeNode]:
252252 """inorder の [lo, hi] 範囲に対応する部分木を再帰構築する"""
253253 nonlocal preorder_idx
254254
Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
155155 self,
156156 inorder: List[int],
157157 postorder: List[int]
158- ) -> Optional[TreeNode]:
158+ ) -> Optional[TreeNode]:
159159 # Python のデフォルト再帰上限は 1000。
160160 # 完全偏り木では n=3000 段の再帰が必要になるため上限を引き上げる。
161161 sys.setrecursionlimit(10_000)
@@ -169,9 +169,9 @@ <h2 class="outfit text-[1.9rem] font-bold text-cyan-700 mb-4 pb-2 border-b-[3px]
169169 # リストに包むことで「リストの中身を変える」操作になり nonlocal 不要になる。
170170 post_idx: List[int] = [len(postorder) - 1]
171171
172- def dfs(left: int, right: int) -> Optional[TreeNode]:
172+ def dfs(left: int, right: int) -> Optional[TreeNode]:
173173 # 終了条件:左端が右端を超えた = この範囲に要素がない = 部分木なし
174- if left > right:
174+ if left > right:
175175 return None
176176
177177 # postorder の末尾から現在の部分木のルートを取り出す。
@@ -1066,7 +1066,9 @@ <h3 className="mt-0 text-teal-800 text-base font-bold mb-2">{currentStepData.tit
10661066root . render ( React . createElement ( StepsApp ) ) ;
10671067
10681068// Re-highlight code after React renders
1069- setTimeout ( ( ) => { if ( window . Prism ) Prism . highlightAll ( ) ; } , 300 ) ;
1069+ requestAnimationFrame ( ( ) => {
1070+ if ( window . Prism ) Prism . highlightAllUnder ( document . getElementById ( "react-steps" ) ) ;
1071+ } ) ;
10701072</ script >
10711073</ body >
10721074</ html >
Original file line number Diff line number Diff line change @@ -829,7 +829,7 @@ <h1 class="site-title">
829829
830830 < footer >
831831 < span class ="footer-icon "> 🧪</ span >
832- Generated on 2026-04-26
832+ Generated on 2026-04-27
833833 </ footer >
834834
835835 < script >
You can’t perform that action at this time.
0 commit comments