Conversation
mamo3gr
reviewed
Feb 7, 2026
| def merge_node(child1, child2): | ||
| if child1 is None and child2 is None: | ||
| return | ||
| if child1 and child2: |
There was a problem hiding this comment.
[fyi]
Googleのスタイルガイドでは、(not) None 判定にはimplicit falseを使わないことが推奨されています。
https://google.github.io/styleguide/pyguide.html#2144-decision
Always use if foo is None: (or is not None) to check for a None value.
mamo3gr
reviewed
Feb 7, 2026
Comment on lines
+41
to
+48
| if node1 is None: | ||
| node1 = TreeNode() | ||
| if node2 is None: | ||
| node2 = TreeNode() | ||
|
|
||
| for side in ("left", "right"): | ||
| child1 = getattr(node1, side) | ||
| child2 = getattr(node2, side) |
There was a problem hiding this comment.
node1 がNoneのときでもleft(right)を引きたい、というのにTreeNodeを使うのは冗長に感じました。
Suggested change
| if node1 is None: | |
| node1 = TreeNode() | |
| if node2 is None: | |
| node2 = TreeNode() | |
| for side in ("left", "right"): | |
| child1 = getattr(node1, side) | |
| child2 = getattr(node2, side) | |
| for side in ("left", "right"): | |
| child1 = getattr(node1, side) if node1 is not None else None | |
| child2 = getattr(node2, side) if node2 is not None else None |
mamo3gr
reviewed
Feb 7, 2026
|
|
||
| merged_child = merge_node(child1, child2) | ||
| if merged_child: | ||
| setattr(frontier_node, side, merged_child) |
There was a problem hiding this comment.
個人的な感覚としては、getattr や setattr は何か特別な事情があるときの手段で、似たようなコードの重複を減らすために使うのは過剰に感じました。
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.
解く問題
Merge Two Binary Trees
次に解く問題
Convert Sorted Array To Binary Search Tree