-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #25
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ## 考察 | ||
| - 初見の問題 | ||
| - 方針 | ||
| - 真ん中がroot、その左側がleft subtree、右側がright subtreeとなるので十分 | ||
| - あとは再帰的に構築できる | ||
| - height-balancedなので、木の高さはlogオーダー | ||
| - よって再帰処理で書いてOK | ||
| - time: O(n), space: O(n) | ||
| - dummyの値を持ったcomplete binary treeを構築して、inorder traversalしながら値をセットしても良さそう | ||
| - BSTをinorder traversalすると、昇順ソートになるため | ||
| - 1ステップ挟むため少しコードが長くなりそう | ||
| - time: O(n), space: O(n) | ||
| - まずは最初の方針を実装して、Step2でもう一方のアイデアをやってみる | ||
|
|
||
| ## Step1 | ||
| - 上で考えたアルゴリズムを実装 | ||
| - time: O(n), space: O(n) | ||
|
|
||
| ## Step2 | ||
| - 上で考えたもう一方のアルゴリズムも実装 | ||
| - 他の人のPRを検索 | ||
| - https://github.com/fhiyo/leetcode/pull/26 | ||
| - 方針はだいたい良さそう | ||
| - 半開区間でやっている | ||
| - これは二分探索というのかな? | ||
| - https://github.com/fhiyo/leetcode/pull/26#discussion_r1651955390 | ||
| - たしかに見る区間を半々にはしているが | ||
| - midという形容詞について | ||
| - https://github.com/fhiyo/leetcode/pull/26#discussion_r1654034973 | ||
| - Boxについて | ||
| - https://github.com/fhiyo/leetcode/pull/26#discussion_r1652886883 | ||
| - 初めて見た | ||
| - 参照をラップしている?一般的な概念なのかな?後ほど詳しく調べる | ||
|
Comment on lines
+30
to
+33
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. 一般的な概念なのかはよく分からないです...オブジェクトを持てればListでも何でも構わないですが、何となくこのときは独自クラスを作りました
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. 元ネタはRustなんですね。ありがとうございます! 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. Box はもうちょっと広い古い概念かとおもいます。Java でもよく使ってましたね。 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. ありがとうございます。Boxing, ふわっとしたイメージしか無かったのでありがたいです |
||
| - https://github.com/Mike0121/LeetCode/pull/13 | ||
| - 方針はだいたい良さそう | ||
| - 閉区間、半開区間ともに書いている | ||
|
|
||
| ## Step3 | ||
| - 1回目: 2m30s | ||
| - 2回目: 1m40s | ||
| - 3回目: 1m37s | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # 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 build_bst(left: int, right: int) -> Optional[TreeNode]: | ||
| if right - left < 0: return None | ||
| mid = (left + right) // 2 | ||
| root = TreeNode(nums[mid]) | ||
| root.left = build_bst(left, mid - 1) | ||
| root.right = build_bst(mid + 1, right) | ||
| return root | ||
|
|
||
| return build_bst(0, len(nums) - 1) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 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: | ||
|
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. こちらの解法は思いつきませんでした。おそらく step1・3 が想定解な気がします。 |
||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def build_complete_binary_tree(index: int) -> Optional[TreeNode]: | ||
| if index >= len(nums): return None | ||
| node = TreeNode() | ||
| node.left = build_complete_binary_tree(2 * index + 1) | ||
| node.right = build_complete_binary_tree(2 * index + 2) | ||
| return node | ||
|
|
||
| binary_tree = build_complete_binary_tree(0) | ||
| index = 0 | ||
| def set_value(root: Optional[TreeNode]) -> None: | ||
| nonlocal index | ||
| if not root: return | ||
| set_value(root.left) | ||
| root.val = nums[index] | ||
| index += 1 | ||
| set_value(root.right) | ||
|
|
||
| set_value(binary_tree) | ||
| return binary_tree | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # 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 build_bst(left: int, right: int) -> Optional[TreeNode]: | ||
| if right - left == 0: return 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.
|
||
| mid = (left + right) // 2 | ||
| root = TreeNode(nums[mid]) | ||
| root.left = build_bst(left, mid) | ||
| root.right = build_bst(mid + 1, right) | ||
| return root | ||
|
|
||
| return build_bst(0, len(nums)) | ||
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.
あ、なるほど〜
inorder traversalの性質をうまく使ってますね 👀