Skip to content

617.merge two binary trees#22

Open
nicah4o wants to merge 1 commit into
mainfrom
617.merge-two-binary-trees
Open

617.merge two binary trees#22
nicah4o wants to merge 1 commit into
mainfrom
617.merge-two-binary-trees

Conversation

@nicah4o
Copy link
Copy Markdown
Owner

@nicah4o nicah4o commented May 28, 2026


BFSのこの方法を写させてもらった。
番兵を使うことでnullptrのleftなどへのアクセスを可能にしているほか、TreeNodeのポインタのポインタを使用することで参照渡しを可能にしている。
(キューだとFIFOなので上から新しい木を作ることになるが、単純にTreeNode *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。)
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 *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。

TreeNode* nodeという変数には何が格納されていますか?

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.

レビューありがとうございます。
nodeにはTreeNode型オブジェクトのポインタが入っています。
この文脈で言えば、親から渡された->leftと->rightのポインタの写しが入っています。->leftの->valをいじることはできますが、->left自体を変更することはできません。

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

Choose a reason for hiding this comment

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

単純にTreeNode *nodeをnode->val=と代入するだけでは毎回の代入はwhileの一回分のスタック領域で変更されるだけで親に反映されない。

この文脈で言えば、親から渡された->leftと->rightのポインタの写しが入っています。->leftの->valをいじることはできますが、->left自体を変更することはできません。

違うことを言っているように見えます。

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.

返信が遅くなり申し訳ありません。
ポインタの定義は、変数のアドレスを格納する変数です。
この場合node->valへの代入で値を変更できると言っている後者のほうが正しいです。

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