Open
Conversation
plushn
reviewed
Dec 31, 2025
Comment on lines
+90
to
+91
| max_depth = max(self.maxDepth(node.left), self.maxDepth(node.right)) + 1 | ||
| return max_depth |
There was a problem hiding this comment.
全体的に読みやすかったです。
ここは、return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1と1行にまとめてもいいかなと思いました。
5ky7
reviewed
Jan 2, 2026
| return 0 | ||
|
|
||
| max_depth = 1 | ||
| stack = [(root, 1)] |
There was a problem hiding this comment.
(Step 2で言及されていますが)一時スタックであっても中身のわかる変数名が良いと感じました.
Comment on lines
+25
to
+31
| while stack: | ||
| node, depth = stack.pop() | ||
| if node.left is not None: | ||
| stack.append((node.left, depth + 1)) | ||
| if node.right is not None: | ||
| stack.append((node.right, depth + 1)) | ||
| max_depth = max(max_depth, depth) |
There was a problem hiding this comment.
こちらのコードでは先にnullチェックを行っていますが,とりあえずスタックに追加してからnullチェックをする手もあります:
while stack:
node, depth = stack.pop()
if not node:
continue
stack.append((node.left, depth + 1))
stack.append((node.right, depth + 1))
max_depth = max(max_depth, depth)どちらが良いかは問題次第と思いますが,今回はどちらでも大差ないでしょう.
| return 0 | ||
|
|
||
| max_depth = 1 | ||
| frontiers = deque() |
oda
reviewed
Jan 4, 2026
Comment on lines
+64
to
+67
| frontiers = [root] | ||
| depth = 0 | ||
| while frontiers: | ||
| depth += 1 |
There was a problem hiding this comment.
ここちょっと難しいですね。[root] の中身があることによって、root の depth が1だと分かるわけですね。
とはいえ、あまり解決策があるわけではないですが。Step3 のほうが明瞭ではあると思います。
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.
解く問題
Maximum Depth of Binary Tree
次に解く問題
Minimum Depth of Binary Tree