-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/111. Minimum Depth of Binary Tree #22
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
- 最後にエラー処理を入れているがあまり同じことをしてる人はいなさそうなので不要かもしれない | ||
- 木構造の高さの測り方として行きがけと帰りがけ・上から配ると下から集めるといった表現をしている人がいた | ||
- 高さを知りたいだけなら行きがけで良いのかなと感じた | ||
- あまり帰りがけのメリットが分からなかった |
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.
高さを知りたいだけならば、行きでよいのは確かです。
もっとも、色々複雑なことをしないといけない場合はあるので、ある程度幅広く選択肢が見えた上で、どうしたいかを選択したほうがいいですね。
変わり種で、こういう書き方もありますよ。(帰りがけをループに直したもの。)
potrue/leetcode#22 (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.
読みやすかったです。これといって気になるところはありませんでした。
def minDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
remaining_nodes_and_depths: List[Tuple[TreeNode, int]] = deque([(root, 1)]) |
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.
好みだとは思いますが、個人的には 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)]) |
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 ではない、の2点で書かなくても良いかなと思いました。型を deque[tuple[TreeNode, int]] のように書くのはNGなのでしょうか?
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.
こちらの書き方でも問題ないようでした。ご指摘ありがとうございます。
個人的には中に変な型を入れたときに型チェックしてくれると嬉しいのでリストやdequeなどは型を書いておこうと思っています
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.
ちなみにどういう型チェックを想定されていますか?
いいとおもいます! |
問題文:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/