Skip to content

617 merge two binary trees#23

Open
kitano-kazuki wants to merge 5 commits intomainfrom
617-merge-two-binary-trees
Open

617 merge two binary trees#23
kitano-kazuki wants to merge 5 commits intomainfrom
617-merge-two-binary-trees

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

while frontier:
next_frontier = []
while frontier:
node1, node2, merged_node = frontier.pop()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空の TreeNode Object を使ってスタックに入れよりも、merged_val を使って TreeNode を初期化した方がループ回数は少なくなると思いました

Copy link
Copy Markdown
Owner Author

@kitano-kazuki kitano-kazuki Mar 19, 2026

Choose a reason for hiding this comment

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

merged_valを使ってTreeNodeを初期化

っていうのは、僕がstep1でやっていたような先にノードを作ってあとからleft, rightを繋ぎかえるようなやり方のことを言っていますか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step 1のもそうかもしれませんが、step 3でもループ内で右と左を一度に処理して以下のようにできそうだと思いました。(動かしていないので間違っていたらすみません。)

node1_left, node2_left = get_left(node1), get_left(node2)
 if node1_left is not None or node2_left is not None:
    m_val = get_merged_val(node1_left, node2_left)
    merged_node.left = TreeNode(m_val)
    next_frontier.append((node1_left, node2_left, merged_node.left))
                
 node1_right, node2_right = get_right(node1), get_right(node2)
 if node1_right is not None or node2_right is not None:
    m_val = get_merged_val(node1_right, node2_right)
    merged_node.right = TreeNode(m_val)
    next_frontier.append((node1_right, node2_right, merged_node.right))

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

理解しました。ありがとうございます。
ご提示いただいた方法にした場合は
merged_node.val = get_merged_val(node1, node2)
は不要になりそうですね。

ループの回数が僕はどちらの方法でも同じだと思ったのですが、具体的にどのようなケースでどのくらい減るのでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

すみません、ループ回数はどちらも全ノード探索で変わりませんね。
ループ回数ではなく、空のオブジェクトを作ることを避けるべきという意図の指摘に訂正させてください。

Copy link
Copy Markdown
Owner Author

@kitano-kazuki kitano-kazuki Mar 19, 2026

Choose a reason for hiding this comment

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

空のオブジェクト=コンストラクタ引数になにも渡さずに作成したインスタンスという認識でいいですかね

避けるべきとする理由は何がありますか?

自分の考えとしてはあえて引数に値を渡していないことでこの後の処理で更新する意図を持たせていたつもりでした。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

はい、その認識です。
「本来の値と異なる 0(デフォルト値)」が一時的にでも入る不自然さを避けたいという意図でした。
間違った値でスタックに積むより、生成時に値を確定させる方が良いと考えての指摘です。
もし好みの範囲であればすみません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

そういうことですね。確かに非確定の値が入ることに一定の不自然さはあるかと思います。
ご指摘ありがとうございます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いえ、不要なツッコミだったかもしれません。
計算量が変わるのは自分の勘違いだったので、気にしなくて大丈夫です。

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