Skip to content

Conversation

@kazukiii
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/

次に解く問題
112. Path Sum

README.mdへ頭の中の言語化と記録をしています。

# self.val = val
# self.left = left
# self.right = right
class Solution:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらの解法は思いつきませんでした。おそらく step1・3 が想定解な気がします。

Copy link

@hayashi-ay hayashi-ay left a 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

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 好みかもしれないですが。自分はこちらのほうが好きです。

Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa left a 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しながら値をセットしても良さそう

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、なるほど〜
inorder traversalの性質をうまく使ってますね 👀

Copy link

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います!

Comment on lines +30 to +33
- Boxについて
- https://github.com/fhiyo/leetcode/pull/26#discussion_r1652886883
- 初めて見た
- 参照をラップしている?一般的な概念なのかな?後ほど詳しく調べる
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般的な概念なのかはよく分からないです...オブジェクトを持てればListでも何でも構わないですが、何となくこのときは独自クラスを作りました
ちなみにBoxの元ネタはRustのBox で、ヒープ上に値を確保するときに使うやつです

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元ネタはRustなんですね。ありがとうございます!

Copy link

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)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。Boxing, ふわっとしたイメージしか無かったのでありがたいです
(Java触ったことあるなら知っていて良い概念ですね...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants