Open
Conversation
Mike0121
reviewed
Jun 29, 2024
| node, depth = queue.popleft() | ||
| if not node.left and not node.right: | ||
| minimum_depth = depth | ||
| break |
Mike0121
reviewed
Jun 29, 2024
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| minimum_depth = 0 |
There was a problem hiding this comment.
BFSで解くのであれば、leaf nodeを見つけた段階でdepthを返してしまって良いので、minimum_depthは不要かと思います。
fhiyo
reviewed
Jun 29, 2024
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| # 幅優先で良い | ||
| minimum_depth = 0 | ||
| queue = deque([(root, 0)]) |
There was a problem hiding this comment.
phase2, 3だと変えてるので違和感あったのかなと思いますが、(node, nodeの一つ上の深さ) の組をキューに入れているのは理解するの難しいなと思いました。
Owner
Author
There was a problem hiding this comment.
そうですね, phase1でわかりにくい+書くなら少しコメントを書く必要があると感じたのでphase2とphase3で変更しました
| if node.right and node.left: | ||
| queue.append((node.right, depth + 1)) | ||
| queue.append((node.left, depth + 1)) | ||
| elif node.right is not None: |
There was a problem hiding this comment.
(phase1のコードに突っ込み入れるのも野暮かもしれませんが) ここだけNoneを使って書いていて他は使ってないので違和感ありました。 elif node.right: で良さそうです
Owner
Author
There was a problem hiding this comment.
ありがとうございます。その通りです...
oda
reviewed
Jun 30, 2024
Comment on lines
+12
to
+15
| if node.left: | ||
| queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| queue.append((node.right, depth + 1)) |
There was a problem hiding this comment.
None であっても気にせずに queue につっこんで、出てきたやつを continue するという手もあります。
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.
問題
111. Minimum Depth of Binary Tree