111. Minimum Depth of Binary Tree#16
Open
hiro111208 wants to merge 1 commit into
Open
Conversation
h-masder
reviewed
Apr 25, 2026
| return 0 | ||
|
|
||
| res = 10 ** 5 | ||
| stack = [[root, 1]] |
There was a problem hiding this comment.
変数名にはどんなものが入っているかを示したほうが分かりやすいかなと思います。
node_and_depthなどはいかがでしょうか。
Owner
Author
There was a problem hiding this comment.
stackの変数名どうするか悩みました。
変数の中身が何かわかりやすくする場合、node_and_depthが良いと思います。他方で今回の処理がDFSであることをわかりやすくするためにはstackにしておいた方が良いとも思いました。
変数の中身のわかりやすさか処理のわかりやすさ(今回の場合DFSをしているとわかりやすく示すこと)のどちらを優先するかだと思います。
h-masder
reviewed
Apr 25, 2026
| if root is None: | ||
| return 0 | ||
|
|
||
| res = 10 ** 5 |
There was a problem hiding this comment.
resの値は、将来変更される可能性が高いと思います。
どんな変数なのか、コメントアウトしておくと親切かと思います。
h-masder
reviewed
Apr 25, 2026
| else: | ||
| stack.append((node.left, depth + 1)) | ||
| stack.append((node.right, depth + 1)) | ||
| return min_depth |
There was a problem hiding this comment.
今回の問題はrootから最も近い葉までの距離を求める問題ですので、
近いものから順に探索していくほうが素直かと思います。
幅優先探索を使って解いてみてもいいかもしれないですね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This problem: 111. Minimum Depth of Binary Tree
Next problem: 617. Merge Two Binary Trees