Skip to content

617. Merge Two Binary Trees#25

Open
xbam326 wants to merge 3 commits into
mainfrom
617
Open

617. Merge Two Binary Trees#25
xbam326 wants to merge 3 commits into
mainfrom
617

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Feb 6, 2026

def merge_node(child1, child2):
if child1 is None and child2 is None:
return
if child1 and child2:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


merged_child = merge_node(child1, child2)
if merged_child:
setattr(frontier_node, side, merged_child)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的な感覚としては、getattrsetattr は何か特別な事情があるときの手段で、似たようなコードの重複を減らすために使うのは過剰に感じました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants