Conversation
|
いいと思います。inorder順に構築する方法もあるみたいです |
olsen-blue
reviewed
Mar 2, 2025
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def index_range_to_BST(left: int, right: int) -> Optional[TreeNode]: | ||
| if not left < right: |
There was a problem hiding this comment.
僕はnot使わず書いてましたが、not 使うこちらの方が直感に近いかもですね。
(自然言語で表現すると)区間が存在しないとなるので、こちらの方がすんなりくる気がしました。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。二分探索もそうですが、これとかも、区間の取り方と成立不成立によって'='がついたりつかなかったりしてややこしいので、notを入れて統一するのが好みかもしれません
There was a problem hiding this comment.
わかります。探索アルゴリズムのグリッド範囲外判定とかも not でやりたい気持ちです。
There was a problem hiding this comment.
ここらへんは趣味かと思います。私は、left >= right と左が大きいときには否定的な意味としています。
Owner
Author
|
@SuperHotDogCat コメントありがとうございます。 |
hroc135
reviewed
Mar 8, 2025
|
|
||
| middle = (left + right) // 2 | ||
| left = index_range_to_BST(left, middle) | ||
| right = index_range_to_BST(middle + 1, right) |
There was a problem hiding this comment.
下でコメントされていますが、int型の引数leftをTreeNodeとしても用いるのはあまり良くないと思います。
fuga-98
reviewed
Mar 17, 2025
| ```python | ||
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def index_range_to_BST(left: int, right: int) -> Optional[TreeNode]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/