Skip to content

617. Merge Two Binary Trees#28

Open
Yuto729 wants to merge 1 commit intomainfrom
merge-two-binary-trees
Open

617. Merge Two Binary Trees#28
Yuto729 wants to merge 1 commit intomainfrom
merge-two-binary-trees

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Dec 31, 2025

@Yuto729 Yuto729 changed the title Merge Two Binary Trees 617. Merge Two Binary Trees Dec 31, 2025
Repository owner deleted a comment from github-actions bot Jan 12, 2026
- スタックは1つにする.スタックに(新しい木の現在ノード, roo1のノード, root2のノード)を入れる.
スタックに格納されている3つの要素は同じ位置のノードを表す.

時間計算量: O(min(m, n)), 空間計算量: O(min(m, n))
Copy link

Choose a reason for hiding this comment

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

こうなる理由も書き留めておくと良いと思いました。m, n はそれぞれ木のノード数でしょうか。
どうにしろすべてのノードを見に行く必要があるので、minは取れなさそうな気がしました。

Comment on lines +23 to +26
if node.left is None:
left = None
else:
left = node.left
Copy link

Choose a reason for hiding this comment

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

個人的には新しい変数は一番上の(浅い)インデントで出てきたほうが驚きが小さいです。

Suggested change
if node.left is None:
left = None
else:
left = node.left
left = None
if node.left is not None:
left = node.left
Suggested change
if node.left is None:
left = None
else:
left = node.left
left = None if node.left is None else node.left

Comment on lines +221 to +233
merged = TreeNode()
root1_left = None
root1_right = None
root2_left = None
root2_right = None
if root1 is not None:
merged.val += root1.val
root1_left = root1.left
root1_right = root1.right
if root2 is not None:
merged.val += root2.val
root2_left = root2.left
root2_right = root2.right
Copy link

Choose a reason for hiding this comment

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

None で初期化される変数がいきなり4つも出てきて身構えてしまいました。それぞれ扱う場所の近くに置くのはどうでしょうか。

Suggested change
merged = TreeNode()
root1_left = None
root1_right = None
root2_left = None
root2_right = None
if root1 is not None:
merged.val += root1.val
root1_left = root1.left
root1_right = root1.right
if root2 is not None:
merged.val += root2.val
root2_left = root2.left
root2_right = root2.right
merged = TreeNode()
root1_left = None
root1_right = None
if root1 is not None:
merged.val += root1.val
root1_left = root1.left
root1_right = root1.right
root2_left = None
root2_right = None
if root2 is not None:
merged.val += root2.val
root2_left = root2.left
root2_right = root2.right

あるいは「mergedに値を足し込む」のと「(あれば)子ノードを取り出す」という作業に分離するのも良いかもしれません。(同じ条件分岐が繰り返されるのがやや冗長かもしれませんが)

Suggested change
merged = TreeNode()
root1_left = None
root1_right = None
root2_left = None
root2_right = None
if root1 is not None:
merged.val += root1.val
root1_left = root1.left
root1_right = root1.right
if root2 is not None:
merged.val += root2.val
root2_left = root2.left
root2_right = root2.right
merged = TreeNode()
if root1 is not None:
merged.val += root1.val
if root2 is not None:
merged.val += root2.val
root1_left = None
root1_right = None
if root1 is not None:
root1_left, root1_right = root1.left, root1.right
root2_left = None
root2_right = None
if root2 is not None:
root2_left, root2_right = root2.left, root2.right

if root2 is None:
return root1

def get_merged_child(node, child1, child2, attr): # attr = "left" or "right"
Copy link

Choose a reason for hiding this comment

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

typing.Literal を使うと型チェッカーでも検査できるようになります。

Suggested change
def get_merged_child(node, child1, child2, attr): # attr = "left" or "right"
def get_merged_child(node, child1, child2, attr: Literal["left", "right"]):

https://typing.python.org/en/latest/spec/literal.html

setattr(node, attr, merged_child)
return merged_child

merged_head = TreeNode(root1.val + root2.val)
Copy link

Choose a reason for hiding this comment

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

head という新しい単語が出てきたからには、rootとは違う何か新しい役割があるのだろうな?」と思いました。既出の root の方が「単なるマージ先」であることが伝わりやすそうです。

Suggested change
merged_head = TreeNode(root1.val + root2.val)
merged_root = TreeNode(root1.val + root2.val)

if root1 is None and root2 is None:
return None

if root1 is None:
Copy link

Choose a reason for hiding this comment

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

@Yuto729
本来レビューで質問するのは良くないと思うのですが、一点だけ確認させてください。
step1ではroot1, root2がNoneである場合はもう一方をそのままReturnしておられたと思うのですが、ここではわざわざ新しくnodeという変数を使って再度改めてmergeTreesまで呼んでいる理由ってなんですか?メモを読んだのですがいまいちここの意図が掴めなくて。。。

Comment on lines +103 to +104
left1, right1 = node1.left, node1.right
left2, right2 = node2.left, node2.right

Choose a reason for hiding this comment

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

自分だったら、ここであえて変数化せずそのまま引数に代入しちゃうと思います

その方がget_merged_childの処理が追いやすいので。

if root2 is None:
return root1

def get_merged_child(node, child1, child2, attr): # attr = "left" or "right"

Choose a reason for hiding this comment

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

child1, child2 の代わりに、child, the_other_child とするのはいかがでしょう?

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.

4 participants