Skip to content

Conversation

Satorien
Copy link
Owner

@Satorien Satorien commented Jun 4, 2025

if middle + 1 < right:
node.right = sortedArrayToBSTHelper(TreeNode(), middle + 1, right)
return node
return sortedArrayToBSTHelper(TreeNode(), 0, len(nums))
Copy link

Choose a reason for hiding this comment

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

TreeNode は引数で与えず中で作ってもこの場合は大丈夫でしょうかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

本当ですね。後からreturnしてくれば先につなげなくてもいいですね

while node_and_range:
node, left_index, right_index = node_and_range.popleft()
middle_index = (left_index + right_index) // 2
node.val = nums[middle_index]
Copy link

Choose a reason for hiding this comment

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

一応、numsが空配列の場合ここでIndexErrorになりますかね。別解ではNoneを返しているので意図通りかは少し気になりました

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: TreeNode = TreeNode(val=nums[0])
nodes_to_add: deque[TreeNode] = deque([root])
for i in range(1, len(nums)):
node = nodes_to_add.popleft()

Choose a reason for hiding this comment

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

nodes_to_add という変数名で、同じノードを入れたり出したりしているのがわかりづらいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

nodes_to_processとかの方が良いですかね

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants