-
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
Open
05ryt31
wants to merge
1
commit into
main
Choose a base branch
from
feat/108
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| 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ではないのでは? |
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
| 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 | ||
|
|
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
| 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 最適解のコード | ||
|
|
||
| # 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]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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),
)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
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.
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.
自分にしっくり来る形で整理されたもの、くらいのニュアンスだと思うのですが、他の人のコードやコメント集を見てさらに選択肢の幅を広げたり、パフォーマンスなどの他の観点でも考察してみるとより味わえると思います。
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.
感覚を一致させることが最終ゴールとは思いますが、やり方は色々だろうと思います。
(人のコードを読む方に重点を置くといいかもしれません。)
Uh oh!
There was an error while loading. Please reload this page.
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.
毎度Google Docsで該当の問題のコメント集に目は通すのですが、他の人のコードももっと見にいくよう心がけます。
ちなみにここの手順ってどのようにされていますか?
特に他の人が書いているコードの探し方などが気になります。
@mamo3gr
cc @oda
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.
横から失礼します。
決まった手順があるわけではないと思いますが、私はDiscordの レビュー依頼チャンネル で自分の解いた問題を検索して、他の方々のpull requests を見るようにしています。
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.
私も、Discordのレビュー依頼チャンネルを問題名で検索して、同じ言語のPRを直近3-5個見るようにしています(実は別な言語を見たほうが更に学びになりそうですが、実践できていません)。
繰り返すうち、考察がしっかりしていたり、好みが自分と似ていたりしている人を見つけるので、その人のPRは必ず入れるようにしています。
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.
お二人ともありがとうございます!これから実践していこうと思います。
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.
完走した人から見繕っておくといいように思います。
「in: レビュー依頼 55」で Discord 検索するとだいたい完走した人がでてきますね。