-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
modify KaTeX reps
| return root | ||
|
|
||
| return helper(0, len(nums) - 1) | ||
| ``` |
There was a problem hiding this comment.
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` とするのもあり? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
start, stop にするのもありだと思いました。
|
いいと思いました。 |
| ```py | ||
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def helper(left, right): |
There was a problem hiding this comment.
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:]) |
There was a problem hiding this comment.
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]
108. Convert Sorted Array to Binary Search Tree
次回予告: 111. Minimum Depth of Binary Tree