Skip to content

Conversation

@skypenguins
Copy link
Owner

108. Convert Sorted Array to Binary Search Tree

次回予告: 111. Minimum Depth of Binary Tree

@skypenguins skypenguins self-assigned this Jul 26, 2025
modify KaTeX reps
return root

return helper(0, len(nums) - 1)
```

Choose a reason for hiding this comment

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

読みやすかったです。

- C++
- DFS
* `TreeNode`の `left` と `right` 、配列インデックスの `left` と `right` で一瞬混乱してしまう
- 後者を `left_i` 、 `right_i` とするのもあり?

Choose a reason for hiding this comment

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

start, stop にするのもありだと思いました。

@quinn-sasha
Copy link

いいと思いました。

```py
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def helper(left, right):

Choose a reason for hiding this comment

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

あまり外部での利用を想定していないのでよいかもしれませんが、sorted_array_to_bst_helper くらいの名前でもよいのかなと思いました。

root = TreeNode(nums[mid])

root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid+1:])

Choose a reason for hiding this comment

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

root.right = self.sortedArrayToBST(nums[mid + 1:])

とスライスのところスペースを開けるべきかとおもったのですがこのあたり全部許容なんですね。
https://peps.python.org/pep-0008/#pet-peeves

# Correct:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants