Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 108-convert-sorted-array-to-binary-search-tree/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## step1で考えたこと
root nodeをどう決めるか? => numsの中央値を取る?

Binary Searchが使えるわけではなさそう?

middleが中心のIndexとなるからそれの一個ずらした左右のIndexをleft, rightとして、端に寄せていくイメージ

## step1に対するフィードバック
- bst.left = nums[left] / bst.right = nums[right] って int を入れてしまっていて、TreeNode をつないでない(木になってない)
- そもそもどんどん左に、右にnodeを増やしていったらそれはHeight-Balancedではないのでは?
31 changes: 31 additions & 0 deletions 108-convert-sorted-array-to-binary-search-tree/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# step1で書いたコード(動かないコード)

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
middle = len(nums) // 2
bst = TreeNode(nums[middle])
# middleの両隣のindexをleft, rightとする
left = middle - 1
right = middle + 1

while left > 0 or right < len(nums) - 1:
if left < 0:
break
else:
bst.left = nums[left]
left -= 1

if right > len(nums) - 1:
break
else:
bst.right = nums[right]
right += 1

return bst

22 changes: 22 additions & 0 deletions 108-convert-sorted-array-to-binary-search-tree/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# step1の書き方に一番近い、正解のコード
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return None

middle = len(nums) // 2
bst = TreeNode(nums[middle])

left_nums = nums[:middle]
right_nums = nums[middle + 1:]

bst.left = self.sortedArrayToBST(left_nums)
bst.right = self.sortedArrayToBST(right_nums)

return bst
20 changes: 20 additions & 0 deletions 108-convert-sorted-array-to-binary-search-tree/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 最適解のコード
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分にしっくり来る形で整理されたもの、くらいのニュアンスだと思うのですが、他の人のコードやコメント集を見てさらに選択肢の幅を広げたり、パフォーマンスなどの他の観点でも考察してみるとより味わえると思います。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感覚を一致させることが最終ゴールとは思いますが、やり方は色々だろうと思います。

(人のコードを読む方に重点を置くといいかもしれません。)

Copy link
Owner Author

@05ryt31 05ryt31 Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

毎度Google Docsで該当の問題のコメント集に目は通すのですが、他の人のコードももっと見にいくよう心がけます。
ちなみにここの手順ってどのようにされていますか?
特に他の人が書いているコードの探し方などが気になります。
@mamo3gr
cc @oda

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

横から失礼します。
決まった手順があるわけではないと思いますが、私はDiscordの レビュー依頼チャンネル で自分の解いた問題を検索して、他の方々のpull requests を見るようにしています。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私も、Discordのレビュー依頼チャンネルを問題名で検索して、同じ言語のPRを直近3-5個見るようにしています(実は別な言語を見たほうが更に学びになりそうですが、実践できていません)。
繰り返すうち、考察がしっかりしていたり、好みが自分と似ていたりしている人を見つけるので、その人のPRは必ず入れるようにしています。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お二人ともありがとうございます!これから実践していこうと思います。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

完走した人から見繕っておくといいように思います。

「in: レビュー依頼 55」で Discord 検索するとだいたい完走した人がでてきますね。


# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def buildBST(left: int, right: int) -> Optional[TreeNode]:
if left > right:
return None

middle = (left + right) // 2
root = TreeNode(nums[middle])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私は TreeNode が中途半端な状態でいるよりも、TreeNodeのインスタンスを作るときにそのままval, left, rightを全て渡してしまう方が好みですが、趣味の範囲だと思います。

return TreeNode(
    val=nums[middle],
    left=buildBST(left, middle - 1),
    right=buildBST(middle + 1, right),
)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに、このように書いた方が綺麗にまとまっていて読みやすいように感じました

root.left = buildBST(left, middle - 1)
root.right = buildBST(middle + 1, right)
return root
return buildBST(0, len(nums) - 1)