Skip to content

Conversation

Satorien
Copy link
Owner

@Satorien Satorien commented Jun 2, 2025

- 最後にエラー処理を入れているがあまり同じことをしてる人はいなさそうなので不要かもしれない
- 木構造の高さの測り方として行きがけと帰りがけ・上から配ると下から集めるといった表現をしている人がいた
- 高さを知りたいだけなら行きがけで良いのかなと感じた
- あまり帰りがけのメリットが分からなかった
Copy link

Choose a reason for hiding this comment

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

高さを知りたいだけならば、行きでよいのは確かです。
もっとも、色々複雑なことをしないといけない場合はあるので、ある程度幅広く選択肢が見えた上で、どうしたいかを選択したほうがいいですね。

変わり種で、こういう書き方もありますよ。(帰りがけをループに直したもの。)
potrue/leetcode#22 (comment)

Copy link

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

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

読みやすかったです。これといって気になるところはありませんでした。

def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
remaining_nodes_and_depths: List[Tuple[TreeNode, int]] = deque([(root, 1)])

Choose a reason for hiding this comment

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

好みだとは思いますが、個人的には nodes_and_depths くらいの長さの変数名でもよいのかなと感じました。比較的コード量が短く処理も簡潔で queue として使っていることがわかりやすいので。

def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
remaining_nodes_and_depths: List[Tuple[TreeNode, int]] = deque([(root, 1)])

Choose a reason for hiding this comment

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

右辺を見たら型はわかる、List ではない、の2点で書かなくても良いかなと思いました。型を deque[tuple[TreeNode, int]] のように書くのはNGなのでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちらの書き方でも問題ないようでした。ご指摘ありがとうございます。
個人的には中に変な型を入れたときに型チェックしてくれると嬉しいのでリストやdequeなどは型を書いておこうと思っています

Choose a reason for hiding this comment

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

ちなみにどういう型チェックを想定されていますか?

@ryoooooory
Copy link

いいとおもいます!

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.

5 participants