Open
Conversation
added 3 commits
September 10, 2024 08:52
|
良さそうです |
Ryotaro25
reviewed
Sep 14, 2024
| treeNodes.addLast(root); | ||
| int maxDepth = 0; | ||
| while (!treeNodes.isEmpty()) { | ||
| int levelNodeCountSnapshot = treeNodes.size(); |
There was a problem hiding this comment.
単にnodeCountか変数に入れないでもtreeNodes.size();で数とわかるので良いかなと思いました。
Owner
Author
There was a problem hiding this comment.
for 文開始時点ではそうなのですが、処理の中で要素数が増える場合に意図しない挙動 (レベルごとに for 文が終わらない) となってしまうので、開始時点の size を何らかの方法で残しておくのは必要だと認識してます。
There was a problem hiding this comment.
これ、WordLadder と同じコメントですが、maxDepth と treeNodes と levelNodeCountSnapshot の関係がかなり密なので、ある瞬間に treeNodes に何が入っているかというのが、自然言語で表現すると複雑になるはずです。
「前から levelNodeCountSnapshot - i 個は maxDepth の深さのノードが入っていて、それより後ろは、maxDepth + 1 の深さのノードが入っている」ということですよね。これを読み解くヒントもあまりないです。
案としては、「maxDepth の深さのノードの入った配列」と「maxDepth + 1 の深さのノードの入った配列」に分けるか「ノードとその深さの組が入った Deque」などやり方はいろいろでしょう。
|
レビュー遅くなりました。全体的に綺麗なコードだと思いました。 |
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.
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/