-
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?
Conversation
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: |
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.
こちらの解法は思いつきませんでした。おそらく step1・3 が想定解な気がします。
hayashi-ay
left a comment
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.
良いと思います。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
if left >= right: return None 好みかもしれないですが。自分はこちらのほうが好きです。
Yoshiki-Iwasa
left a comment
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.
全体的に問題なさそうです
| - height-balancedなので、木の高さはlogオーダー | ||
| - よって再帰処理で書いてOK | ||
| - time: O(n), space: O(n) | ||
| - dummyの値を持ったcomplete binary treeを構築して、inorder traversalしながら値をセットしても良さそう |
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の性質をうまく使ってますね 👀
fhiyo
left a comment
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.
良いと思います!
| - Boxについて | ||
| - https://github.com/fhiyo/leetcode/pull/26#discussion_r1652886883 | ||
| - 初めて見た | ||
| - 参照をラップしている?一般的な概念なのかな?後ほど詳しく調べる |
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.
一般的な概念なのかはよく分からないです...オブジェクトを持てればListでも何でも構わないですが、何となくこのときは独自クラスを作りました
ちなみにBoxの元ネタはRustのBox で、ヒープ上に値を確保するときに使うやつです
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.
元ネタはRustなんですね。ありがとうございます!
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.
Box はもうちょっと広い古い概念かとおもいます。Java でもよく使ってましたね。
https://en.wikipedia.org/wiki/Boxing_(computer_science)
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.
ありがとうございます。Boxing, ふわっとしたイメージしか無かったのでありがたいです
(Java触ったことあるなら知っていて良い概念ですね...)
問題へのリンク
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
次に解く問題
112. Path Sum
README.mdへ頭の中の言語化と記録をしています。