-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #26
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
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
30 changes: 30 additions & 0 deletions
30
problems/108.convert-sorted-array-to-binary-search-tree/memo.md
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,30 @@ | ||
| ## step1 | ||
| - 難しく感じるがEasyなのか | ||
| - 配列を前から見て適切な位置に1つ1つ入れていくのがいいのかなと思うが、Example1の状態で-11,-12,-13と入れていく場合はどうすればいいかわからない | ||
| - 平衡である必要がない場合は書けた | ||
| - わからないのでChatGPTに聞く | ||
| - 常に中央の値を親にして小さい値を左、大きい値を右に再帰していく | ||
| - 常に平衡二分木を保ちつつデータを追加していく(DBのIndexで使われているような話を聞いたことがある)ようにできた方が良いと勝手に思い込んで先頭から足す方法にこだわってしまった | ||
|
|
||
|
|
||
| ## step2 | ||
| - 他の人のコードを読む | ||
| - https://github.com/05ryt31/leetcode/pull/15 | ||
| - コードの出力は自分と似ていたので感覚は近そう | ||
| - https://github.com/mamo3gr/arai60/pull/23/changes | ||
| - 配列の範囲の終点を扱うのに、開区間と閉区間の方がある(始点は閉区間でいいだろう)。Pythonのインデックスに従うなら開区間だが、閉区間の方が(個人的には)扱いやすくバグのリスクが減らせそう。「リーダブルコードで」、開区間なら `begin, end`, 閉区間なら `start, last` を使う、という命名を思い出す。→書いてみたら「中央の要素を抜いた、配列の左右がそれぞれ存在するかどうか」の分岐条件が違うだけみたいだった。 | ||
| - 命名の部分勉強になった | ||
| - https://github.com/Shoichifunyu/shofun/pull/18 | ||
| - 外部要因とかやむを得ない部分以外でtry-catchを使うことが自分的には違和感があった | ||
| - 自分が制御できる範囲や正常系の範囲の中でErrorを使用するのに自分は抵抗がある感じ | ||
| - `build_tree関数の返り値の型がTreeNode(非Optional)になるように書くこともでき、再帰呼び出しの回数がおよそ半減します。趣味の範囲です。` | ||
| - buildを使う再帰の場合Noneを省けるので自分で書く際には条件が絞られ思考がシンプルになりそうだと思った | ||
| - 理解しながら書いてみた | ||
| - 配列ごと分けるのかndexで分けるのかでも違いがある | ||
| - 取り組む際に早めにあきらめてChatGPTに答えを聞いてしまったことで受け身で学ぶ体験になってしまったので、無理矢理でも途中まででも自分でコードを書けばよかったと思った | ||
|
|
||
| ## step3 | ||
| - 一度慣れると自然な感じがしてきた | ||
| - sliceの範囲が含むかどうかを毎回Interfaceモードを起動して具体例から調べてしまうので早く慣れたい | ||
| - a[0,1,2,3,4,5]としたときに a[:2]が[0,1]なのか[0,1,2]なのか毎回わからなくなる。。。 | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
problems/108.convert-sorted-array-to-binary-search-tree/step1.py
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,28 @@ | ||
| # | ||
| # @lc app=leetcode id=108 lang=python3 | ||
| # | ||
| # [108] Convert Sorted Array to Binary Search Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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 | ||
|
|
||
| mid = len(nums) // 2 | ||
| root = TreeNode(nums[mid]) | ||
|
|
||
| root.left = self.sortedArrayToBST(nums[:mid]) | ||
| root.right = self.sortedArrayToBST(nums[mid + 1 :]) | ||
|
|
||
| return root | ||
|
|
||
|
|
||
| # @lc code=end |
39 changes: 39 additions & 0 deletions
39
problems/108.convert-sorted-array-to-binary-search-tree/step1_wrong_answer.py
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,39 @@ | ||
| # | ||
| # @lc app=leetcode id=108 lang=python3 | ||
| # | ||
| # [108] Convert Sorted Array to Binary Search Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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]: | ||
| binary_root = None | ||
|
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. 他と区別する必要がないため、 binary_ という接頭辞は省略しても十分伝わると思いました。 |
||
|
|
||
| def add_binary_node(binary_node, num): | ||
| if num < binary_node.val: | ||
| if binary_node.left is None: | ||
| binary_node.left = TreeNode(num) | ||
| else: | ||
| return add_binary_node(binary_node.left, num) | ||
| else: | ||
| if binary_node.right is None: | ||
| binary_node.right = TreeNode(num) | ||
| else: | ||
| return add_binary_node(binary_node.right, num) | ||
|
|
||
| for num in nums: | ||
| if binary_root is None: | ||
| binary_root = TreeNode(num) | ||
| continue | ||
| add_binary_node(binary_root, num) | ||
|
|
||
| return binary_root | ||
|
|
||
|
|
||
| # @lc code=end | ||
33 changes: 33 additions & 0 deletions
33
problems/108.convert-sorted-array-to-binary-search-tree/step2.py
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,33 @@ | ||
| # | ||
| # @lc app=leetcode id=108 lang=python3 | ||
| # | ||
| # [108] Convert Sorted Array to Binary Search Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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 | ||
|
|
||
| def build_tree(left, right): | ||
| mid = (left + right) // 2 | ||
|
|
||
| root = TreeNode(nums[mid]) | ||
|
|
||
| if left < mid: | ||
| root.left = build_tree(left, mid - 1) | ||
| if mid < right: | ||
| root.right = build_tree(mid + 1, right) | ||
| return root | ||
|
|
||
| return build_tree(0, len(nums) - 1) | ||
|
|
||
|
|
||
| # @lc code=end |
28 changes: 28 additions & 0 deletions
28
problems/108.convert-sorted-array-to-binary-search-tree/step3.py
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,28 @@ | ||
| # | ||
| # @lc app=leetcode id=108 lang=python3 | ||
| # | ||
| # [108] Convert Sorted Array to Binary Search Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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 | ||
|
|
||
| mid = len(nums) // 2 | ||
|
|
||
| root = TreeNode(nums[mid]) | ||
| root.left = self.sortedArrayToBST(nums[:mid]) | ||
| root.right = self.sortedArrayToBST(nums[mid + 1 :]) | ||
|
|
||
| return root | ||
|
|
||
|
|
||
| # @lc code=end |
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.
半開区間なら
begin, endをよく見かけるように感じます。 C++ のイテレーターはこのパターンです。